gpt4 book ai didi

Valid with the RegExp u flag, but not with the v flag(与RegExp u标志一起有效,但不与v标志一起使用)

转载 作者:bug小助手 更新时间:2023-10-25 17:05:56 35 4
gpt4 key购买 nike



I am getting the below console warning for this regex pattern:

我收到以下有关此正则表达式模式的控制台警告:


^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$


Pattern attribute value ^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$ is valid with the RegExp u flag, but not with the v flag: Uncaught SyntaxError: Invalid regular expression: /^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/v: Invalid character in character class.



I can not see how to create a valid regex patter for this warning. Please, could somebody explain the error and how to resolve it?

我看不到如何为该警告创建有效的正则表达式模式。请问,有没有人能解释一下这个错误以及如何解决它?


Tried looking at documentation, but could not see how to make it valid for the v flag

已尝试查看文档,但看不到如何使其对v标志有效


更多回答

How about changing the first - to \-?

把First-改成\-怎么样?

优秀答案推荐

The issue is that the newly introduced v flag applies even more restrictions to escaping rules. Since it allows character class subtraction and intersection, the literal - at the end of a character class cannot be left unescaped.

问题是,新引入的v标志对转义规则施加了更多限制。由于它允许字符类减法和交集,因此字符类末尾的文字-不能保持不转义。


So, if you use the u flag, there is no such a restriction, with the v flag, it is in place. Cf.

所以,如果你使用u标志,就没有这样的限制,使用v标志,它就存在了。参见




console.log(/^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/u.test("[email protected]"))
console.log(/^[a-zA-Z0-9+_.\-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/v.test("[email protected]"))




So, always escape literal hyphens inside character classes in ECMAScript patterns.

所以,在ECMAScript模式中,字符类中的文字连字符总是转义的。


Here are more details on which patterns are now considered invalid:

以下是有关哪些模式现在被视为无效的更多详细信息:



Some previously valid patterns are now errors, specifically those with a character class including either an unescaped special character ( ) [ { } / - | (note: \ and ] also require escaping inside a character class, but this is already true with the u flag) or a double punctuator:


[(]
[)]
[[]
[{]
[}]
[/]
[-]
[|]
[&&]
[!!]
[##]
[$$]
[%%]
[**]
[++]
[,,]
[..]
[::]
[;;]
[<<]
[==]
[>>]
[??]
[@@]
[``]
[~~]
[^^^]
[_^^]


更多回答

Good to know. This subsection in the link you've given specifically talks about the issue at hand: How is the v flag different from the u flag?.

很高兴知道。您给出的链接中的这一小节专门讨论了手头的问题:v标志与u标志有什么不同?

@oriberu I added these pertinent details to the question, that will be surely helpful.

@oriberu我在问题中加入了这些相关的细节,这肯定会有帮助。

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com