简体   繁体   English

当正则表达式有效并在其他地方工作时,带有“ m”标志的JavaScript正则表达式错误,“无效的正则表达式组”

[英]Javascript regex error with 'm' flag, 'invalid regexp group' when regex is valid and working elsewhere

This line of regex is breaking in javascipt, however when using a regex tester it works correctly: 这行正则表达式在javascipt中中断,但是使用正则表达式测试器时,它可以正常工作:

var pattern = new RegExp(/^(?m)^([A-Z0-9]{2,4})(?:\s*[A-Z0-9]{3})?$/);
"SL44BZ".match(pattern);

How do I make this work with javascript's .match()? 如何使用javascript的.match()进行此操作?

It should return an array of two results splitting at the 'SL4'. 它应返回两个结果数组,在“ SL4”处拆分。

It looks like this issue is with the multiline '(?m)' flag, however if I remove this wrong results are returned. 看来此问题与多行'(?m)'标志有关,但是,如果我删除此错误结果,则会返回。

var pattern = /^([A-Z0-9]{2,4})(?:\\s*[A-Z0-9]{3})?$/m; "SL44BZ".match(pattern);

如果您还没有从评论中找出答案,则应该这样做。

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

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