Javascript searcing and replacing?
I want to change a string which start 'a' and end 'n'.
For example: "action" I want to replace 'ctio' a开发者_如何学Pythonnd all starting 'a' and finishing 'n' with ''.
How can do it?
return theString.replace(/\ba[a-z]*n\b/ig, '')
in Javascript:
var substitute = "\"";
var text = "action";
var text = text.replace(/\b(a)([a-z]+?)(n)\b/gim,"$1" + substitute + "$3");
// result = a"n ... if what you really want is a double quote here
I'm not really sure what you're trying to do, but I'm guessing going from "action" to "ctio"?
var foo = 'action';
if (foo.substr(0,1)=='a' && foo.substr(-1,1)=='n') {
var bar = foo.substr(1,foo.length-2);
alert(bar); // ctio
}
try this following
str.replace(/\ba(\w+)n\b/igm,'');
to the question in the comment use th following comment
var sub = "hello";
str.replace(/(<)(\w+)(")/igm,"$1" + sub + "$3");
精彩评论