不匹配 ) 在 Javascript 正则表达式中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7885328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:38:27  来源:igfitidea点击:

Unmatched ) in Javascript regular expression

javascriptregex

提问by XTRUST.ORG

May be you can help me: My javascript code:

也许你可以帮助我:我的javascript代码:

bbchatdecode: function(text) {  

var chars = Array(":\)","8-\)",":cry:",":oops:");
var replacements = Array('<img src=\"smiley-smile.gif\" alt=\":)\">','<img src=\"smiley-cool.gif\" alt=\"8-)\">','<img src=\"smiley-cry.gif\" alt=\":cry:\">','<img src=\"smiley-embarassed.gif\" alt=\"oops:\">');
for (var ic=0; ic<chars.length; ic++) {
  var re = new RegExp(chars[ic], "gi");
  if(re.test(text))
  {
   text = text.replace(re, replacements[ic]);
  }
}
return text;
}   

But in the browser I can see:

但是在浏览器中我可以看到:

unmatched )in regular expression

)在正则表达式中不匹配

回答by Arvind Bhardwaj

Unmatched ) in Javascript regular expressionerror occurs when some of your string contains ')'. You need to escape this. Here is the function to do that:

Unmatched ) in Javascript regular expression当您的某些字符串包含')'. 你需要逃避这个。这是执行此操作的函数:

function escapeRegExp(str) {
  return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\^$\|]/g, "\$&");
}

回答by shybovycha

Valid regexp string is: :\\\). You should triple-repeat backslash in order to escape something from string. When you are constructing your regexp as raw JS (e.g. var re = /:\)/;) you do not to do that.

有效的正则表达式字符串是::\\\)。您应该重复三次反斜杠以从字符串中转义某些内容。当您将正则表达式构建为原始 JS(例如var re = /:\)/;)时,您不要这样做。

Sorry, but i can not explain why this happens for now.

抱歉,我暂时无法解释为什么会发生这种情况。

回答by jmoreno

As with one of the commentors, I don't believe thats your code (excluding the fact that you left out the object declaration). It does't give me that error and jsFiddle makes it work if an object is declared.

与其中一位评论者一样,我不相信那是您的代码(不包括您遗漏了对象声明的事实)。它不会给我那个错误,如果声明了一个对象,jsFiddle 就会使它工作。

I think what is going on is that is actually part of some code in another language (php, perl?) to output an object with that propery, and the multi line string isn't working as expected. Copy the code off of actual page, include a bit more context, and we might be able to solve your problem (if this doesn't lead you to an answer).

我认为正在发生的事情实际上是另一种语言(php,perl?)的某些代码的一部分,用于输出具有该属性的对象,并且多行字符串未按预期工作。从实际页面复制代码,包含更多上下文,我们或许能够解决您的问题(如果这不能引导您找到答案)。

回答by Anja Ishmukhametova

let's say you have var str='ff_!)'and you need var reg = new RegExp(str, 'gi')yes you will get error

假设你有var str='ff_!)'并且你需要var reg = new RegExp(str, 'gi')是你会得到错误

SyntaxError: Invalid regular expression: /ff_!)/: Unmatched ')'

what you actually need is that fix:

你真正需要的是修复:

new RegExp('ff_![(]', 'gi').test('ff_!(') 

the line above return true

上面的行返回 true

or event better this fix

或事件更好这个修复

new RegExp('ff[_][!][(]', 'gi').test('ff_!(')

the line above return truetoo.

上面的行也返回true

solution:

解决方案:

if (/\W|[_]/g.test(str)){ //if str has any symbols
      str = str.replace(/\W|_/g, '[$&]'); //use brekits [ ] for that symbols
    }
var reg = new RegExp(str, 'gi');
reg.test('ff_!(') //return true

about $&(Specifying a string as a parameter) you can read here

关于$&(指定一个字符串作为参数)你可以在这里阅读