javascript 字符类中的Javascript正则表达式无效范围

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16572123/
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-27 05:10:29  来源:igfitidea点击:

Javascript regex invalid range in character class

javascriptregex

提问by bflemi3

I'm using a regex pattern that I got from regexlib to validate relative urls. On their site you can testthe pattern to make sure it fits your needs. Everything works great on their site, as soon as I use the pattern in mine I get the error message:

我正在使用从 regexlib 获得的正则表达式模式来验证相对 url。在他们的网站上,您可以测试该模式以确保它符合您的需求。在他们的网站上一切都很好,只要我使用我的模式,我就会收到错误消息:

Invalid range in character class

字符类中的无效范围

I know that this error usually means that a hyphen is mistakenly being used to represent a range and is not properly escaped. But in this case since it works on their site I'm confused why it's not working on mine.

我知道这个错误通常意味着连字符被错误地用于表示一个范围并且没有正确转义。但在这种情况下,因为它在他们的网站上工作,我很困惑为什么它在我的网站上不起作用。

var urlRegex = new RegExp('^(?:(?:\.\./)|/)?(?:\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)?(?:/\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)*(?:\?[^#]+)?(?:#[a-z0-9]\w*)?$', 'g');

NOTE:If you're going to test the regex from their site (using the link above) be sure to change the Regex Enginedropdown to Client-side Engineand the Enginedropdown to Javascript.

注意:如果你打算从他们的网站测试正则表达式(使用上面的链接),一定要改变Regex Engine下拉菜单Client-side EngineEngine下拉菜单Javascript

回答by Anirudha

Either put -at the endor beginningof the character class oruse two backslashesto do a regex escape within string

要么放在字符类-末尾开头要么使用 两个反斜杠字符串中进行正则表达式转义

since you are using string you need to use two backslashesfor each special characters..

由于您使用的是字符串,因此您需要为每个特殊字符使用两个反斜杠



NOTE

笔记

Check out thisanswer on SO which explains when to use single or double backslashes to escape special characters

查看SO 上的这个答案,它解释了何时使用单反斜杠或双反斜杠来转义特殊字符

回答by nhahtdh

There is no reason to use RegExpconstructor here. Just use RegExpliteral:

这里没有理由使用RegExp构造函数。只需使用RegExp文字:

var urlRegex = /^(?:(?:\.\.\/)|\/)?(?:\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)?(?:\/\w(?:[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]|(?:%\d\d))*\w?)*(?:\?[^#]+)?(?:#[a-z0-9]\w*)?$/g;
               ^           ^   ^                                                               ^                                                                                     ^

Inside RegExpliteral, you just write the regex naturally, except for /, which now needs escaping, since /is used as delimiter in the RegExpliteral.

RegExp文字内部,您只需自然地编写正则表达式,除了/现在需要转义的 ,因为/RegExp文字中用作分隔符。

In character class, ^has special meaning at the beginning of the character class, -has special meaning in between 2 characters, and \has special meaning, which is to escape other characters (mainly ^, -, [, ]and \) and also to specify shorthand character classes (\d, \s, \w, ...). [, ]are used as delimiters for character class, so they also have special meaning. (Actually, in JavaScript, only ]has special meaning, and you can specify [without escaping inside character class). Other than those 5 character listed above, other characters (unless involved in an escape sequence with \) doesn't have any special meaning.

在字符类,^具有在字符类的开头特殊的含义,-在2个字符之间特殊的含义,并\具有特殊的意义,这是逃避其他字符(主要是^-[]\),并且还可以指定速记字符类(\d\s, \w, ...)。[,]用作字符类的分隔符,因此它们也具有特殊含义。(实际上,在 JavaScript 中,只有]特殊含义,可以[在字符类中不转义指定)。除了上面列出的 5 个字符外,其他字符(除非包含在带有 的转义序列中\)没有任何特殊含义。

You can reduce the number of escaping \with the information above. For ^, unless it is the only character in the character class, you can put it away from the beginning of the character class. For -, you can put it at the end of the character class.

您可以\使用上述信息减少转义次数。对于^,除非它是字符类中的唯一字符,否则您可以将其放在字符类的开头。对于-,您可以将其放在字符类的末尾。

var urlRegex = /^(?:(?:\.\.\/)|\/)?(?:\w(?:[\w`~!$=;+.^()|{}\[\]-]|(?:%\d\d))*\w?)?(?:\/\w(?:[\w`~!$=;+.^()|{}\[\]-]|(?:%\d\d))*\w?)*(?:\?[^#]+)?(?:#[a-z0-9]\w*)?$/g;

What was changed:

改变了什么:

[\w`~!$=;\-\+\.\^\(\)\|\{\}\[\]]
[\w`~!$=;+.^()|{}\[\]-]