简体   繁体   English

Javascript替换不替换

[英]Javascript replace does not replace

The pattern in this code does not replace the parenthesis. 这段代码中的模式不会替换括号。 I've also tried "/(|)/g". 我也尝试过“ /(|)/ g”。

var re = "/[^a-z]/g",
   txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
   // What I expect is strings like "(en)" , "(el)" etc
   txt = txt.replace(re," ")

Thanks in advance 提前致谢

Your regex is a string, this will try to replace that exact string. 您的正则表达式是一个字符串,它将尝试替换该确切的字符串。 Regex objects don't have quotes around them, just the delimiters. 正则表达式对象周围没有引号,只有定界符。 Try it like this: 像这样尝试:

var re = /[^a-z]/g,
   txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it.
   txt = txt.replace(re," ");

或者,如果您更喜欢字符串(以及更明确的类型):

var re = new RegExp("[^a-z]", "g")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM