简体   繁体   English

Javascript + Regex = Nothing to repeat 错误?

[英]Javascript + Regex = Nothing to repeat error?

I'm new to Regex and I'm trying to work it into one of my new projects to see if I can learn it and add it to my repitoire of skills.我是 Regex 的新手,我正在尝试将它应用到我的一个新项目中,看看我是否可以学习它并将其添加到我的技能库中。 However, I'm hitting a roadblock here.但是,我在这里遇到了障碍。

I'm trying to see if the user's input has illegal characters in it by using the .search function as so:我试图通过使用.search function 来查看用户输入中是否包含非法字符:

if (name.search("[\[\]\?\*\+\|\{\}\\\(\)\@\.\n\r]") != -1) {
    ...
}

However, when I try to execute the function this line is contained it, it throws the following error for that specific line:但是,当我尝试执行 function 时,此行包含它,它会针对该特定行抛出以下错误:

Uncaught SyntaxError: Invalid regular expression: /[[]?*+|{}\()@.

]/: Nothing to repeat

I can't for the life of me see what's wrong with my code.我这辈子都看不出我的代码有什么问题。 Can anyone point me in the right direction?谁能指出我正确的方向?

You need to double the backslashes used to escape the regular expression special characters.您需要将用于转义正则表达式特殊字符的反斜杠加倍。 However, as @Bohemian points out, most of those backslashes aren't needed.但是,正如@Bohemian 指出的那样,大多数反斜杠都不需要。 Unfortunately, his answer suffers from the same problem as yours.不幸的是,他的答案与您的问题相同。 What you actually want is:你真正想要的是:

The backslash is being interpreted by the code that reads the string, rather than passed to the regular expression parser.反斜杠由读取字符串的代码解释,而不是传递给正则表达式解析器。 You want:你要:

"[\\[\\]?*+|{}\\\\()@.\n\r]"

Note the quadrupled backslash.注意四倍的反斜杠。 That is definitely needed.这绝对是需要的。 The string passed to the regular expression compiler is then identical to @Bohemian's string, and works correctly.传递给正则表达式编译器的字符串与@Bohemian 的字符串相同,并且可以正常工作。

Building off of @Bohemian, I think the easiest approach would be to just use a regex literal, eg:在@Bohemian 的基础上,我认为最简单的方法是使用正则表达式文字,例如:

if (name.search(/[\[\]?*+|{}\\()@.\n\r]/) != -1) {
    // ... stuff ...
}

Regex literals are nice because you don't have to escape the escape character, and some IDE's will highlight invalid regex (very helpful for me as I constantly screw them up).正则表达式文字很好,因为您不必转义转义字符,并且一些 IDE 会突出显示无效的正则表达式(对我很有帮助,因为我经常搞砸它们)。

Firstly, in a character class [...] most characters don't need escaping - they are just literals.首先,在字符 class [...]中,大多数字符不需要 escaping - 它们只是文字。

So, your regex should be:所以,你的正则表达式应该是:

"[\[\]?*+|{}\\()@.\n\r]"

This compiles for me.这为我编译。

For Google travelers: this stupidly unhelpful error message is also presented when you make a typo and double up the + regex operator:对于 Google 旅行者:当您输入错误并将+正则表达式操作符加倍时,也会显示此愚蠢无用的错误消息:

Okay:好的:

\w+

Not okay:不行:

\w++

Well, in my case I had to test a Phone Number with the help of regex, and I was getting the same error,好吧,就我而言,我不得不在正则表达式的帮助下测试一个电话号码,我得到了同样的错误,

Invalid regular expression: /+923[0-9]{2}-(?!1234567)(?!1111111)(?!7654321)[0-9]{7}/: Nothing to repeat'

So, what was the error in my case was that + operator after the / in the start of the regex.因此,在我的情况下,错误是正则表达式开头/之后的+运算符。 So enclosing the + operator with square brackets [+] , and again sending the request, worked like a charm.因此,用方括号[+]+运算符括起来,然后再次发送请求,就像一个魅力。

Following will work:以下将起作用:

/[+]923[0-9]{2}-(?!1234567)(?!1111111)(?!7654321)[0-9]{7}/

This answer may be helpful for those, who got the same type of error, but their chances of getting the error from this point of view, as mine: Cheers :)这个答案可能对那些遇到相同类型错误的人有所帮助,但从这个角度来看,他们有机会获得错误,就像我的一样:干杯:)

This can also happen if you begin a regex with ?如果您使用?开始正则表达式,也会发生这种情况。 . .

? may function as a quantifier -- so ?可能 function 作为量词- 所以? may expect something else to come before it, thus the "nothing to repeat" error.可能期望在它之前出现其他内容,因此出现“无重复”错误。 Nothing preceded it in the regex string so it didn't get to quantify anything;正则表达式字符串中没有任何内容在它之前,所以它没有量化任何东西; there was nothing to repeat / nothing to quantify.没有什么可以重复/没有什么可以量化。

? also has another role -- if the ?还有另一个作用——如果? is preceded by ( it may indicate the beginning of a lookaround assertion or some other special construct. See example below.前面有(它可能表示环视断言或其他一些特殊构造的开始。请参见下面的示例。

If one forgets to write the () parentheses around the following lookbehind assertion ?<=x , this will cause the OP's error:如果忘记在以下回顾断言?<=x周围写上()括号,这将导致 OP 的错误:

Incorrect: const xThenFive = /?<=x5/;不正确: const xThenFive = /?<=x5/;

Correct: const xThenFive = /(?<=x)5/;正确: const xThenFive = /(?<=x)5/;

This /(?<=x)5/ is a positive lookbehind: we're looking for a 5 that is preceded by an x eg it would match the 5 in x563 but not the 5 in x652 .这个/(?<=x)5/是一个积极的后视:我们正在寻找一个前面有一个x的 5 例如它会匹配x563中的5而不是x652中的5

for example I faced this in express node.js when trying to create route for paths not starting with /internal例如,当我尝试为不以/internal开头的路径创建路径时,我在 express node.js 中遇到了这个问题

app.get(`\/(?!internal).*`, (req, res)=>{

and after long trying it just worked when passing it as a RegExp Object using new RegExp()经过长时间的尝试,它只是在使用new RegExp()将它作为 RegExp Object 传递时才起作用

app.get(new RegExp("\/(?!internal).*"), (req, res)=>{

this may help if you are getting this common issue in routing如果您在路由中遇到这个常见问题,这可能会有所帮助

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

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