【发布时间】:2011-02-20 23:28:58
【问题描述】:
我想更改一个以 'a' 开头并以 'n' 结尾的字符串。
例如:“action”我想替换 'ctio' 和所有以 'a' 开始并以 '' 结束的 'n'。
怎么办?
【问题讨论】:
标签: javascript search replace
我想更改一个以 'a' 开头并以 'n' 结尾的字符串。
例如:“action”我想替换 'ctio' 和所有以 'a' 开始并以 '' 结束的 'n'。
怎么办?
【问题讨论】:
标签: javascript search replace
return theString.replace(/\ba[a-z]*n\b/ig, '')
【讨论】:
a和n之间的字母即可。
在 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
【讨论】:
substitute 的字符串即可。
a开头,以n结尾并有另一个@的问题987654325@ 或 a 在单词中间的任何位置。
我不太确定您要做什么,但我猜想从“行动”到“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
}
【讨论】:
试试下面这个
str.replace(/\ba(\w+)n\b/igm,'');
对于评论中的问题,请使用以下评论
var sub = "hello";
str.replace(/(<)(\w+)(")/igm,"$1" + sub + "$3");
【讨论】: