2010-05-18 122 views

回答

4
return theString.replace(/\ba[a-z]*n\b/ig, '') 
+1

这将替换整个单词,他只需要替换'a'和'n'之间的字母。 – 2010-05-18 16:04:43

+0

谢谢我想用'<'和''而不是'a'和'n'我不能这样做 – sundowatch 2010-05-18 17:26:49

+0

@sun:你想剥离HTML标签吗? – kennytm 2010-05-18 18:40:10

2

在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 
+0

,我怎么能取代 'asdasd' 而不是 '' – sundowatch 2010-05-18 15:56:04

+1

@sundowatch:只需更改分配给“替代”的字符串即可。 – 2010-05-18 15:59:52

+1

@Robusto:由于非贪婪量词和缺少单词边界,这种解决方案对于以'a'开头,以'n'结尾且在其他地方具有另一个'n'或'a'的单词有问题在词的中间。 – 2010-05-18 16:02:53

1

我真的不知道你想做什么,但我猜从“行动”,以“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 
}
1

试试这个下面

str.replace(/\ba(\w+)n\b/igm,''); 

在注释使用的问题日以下注释

var sub = "hello"; 
str.replace(/(<)(\w+)(")/igm,"$1" + sub + "$3"); 
+0

你的代码是正确的,但我想用'<'和''而不是'a'和'n',我该怎么办? – sundowatch 2010-05-18 17:18:17