在 grails 配置中实现不区分大小写的正则表达式 (?i)

Implement case insensitive regex (?i) in grails config

我的 grails 配置中有一个正则表达式:

password.regex = /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?!.*0123|.*1234|.*2345|.*3456|.*4567|.*5678|.*6789|.*3210|.*4321|.*5432|.*6543|.*7654|.*8765|.*9876|.*1122|.*2233|.*3344|.*4455|.*5566|.*6677|.*7788|.*8899|.*9900|.*0011|.*1100|.*qwerty)[0-9a-zA-Z]*$/

我在正则表达式中限制了 qwerty 一词。我试图添加 (?i) 以使其不区分大小写 .*(?i)qwerty 但由于某种原因,不区分大小写的正则表达式导致页面使用的 javascript 出错。

我应该把这个不区分大小写的正则表达式放在哪里?我也尝试过转义它 (?\\i) 但它仍然会导致页面中使用的 javascript 出错。有人可以解释为什么吗?

您不能在 JavaScript 中使用 内联正则表达式修饰符 。此外,并非每种风格都让您使用 (?i:...) 构造使模式的一部分不区分大小写。因此,您可以做的事情非常有限:只需使用字符 类 来指定每个单词的两种大小写变体:

.*[qQ][wW][eE][rR][tT][yY]

参见rexegg.com Regex Modifiers—Turning them On参考:

Inline Modifier (?i) In .NET, PCRE (C, PHP, R…), Perl, Python, Java and Ruby (but not JavaScript), you can use the inline modifier (?i), for instance in (?i)cat. See the section on inline modifiers for juicy details about three additional features (unavailable in Python): turning it on in mid-string, turning it off with (?-i), or applying it only to the content of a non-capture group with (?i:foo)

现在,使用 /i 作为 (?i) 的替代方法是使用 XRegExp library:

When creating a regex, it's okay to include flags in a mode modifier that are also provided via the separate flags argument. For instance, XRegExp('(?s).+', 's') is perfectly valid.

但是,似乎无法使模式的一部分不区分大小写:

Compatibility with other regex flavors: Some regex flavors support the use of multiple mode modifiers anywhere in a pattern, and allow extended syntax for unsetting flags via (?-i), simultaneously setting and unsetting flags via (?i-m), and enabling flags for subpatterns only via (?i:…). XRegExp does not support these extended options*.