简体   繁体   中英

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:

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()?

It should return an array of two results splitting at the 'SL4'.

It looks like this issue is with the multiline '(?m)' flag, however if I remove this wrong results are returned.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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