firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.21k stars 198 forks source link

Js flag G is not work as expected #2071

Closed Alexufo closed 1 year ago

Alexufo commented 1 year ago

g modifier does not work as in a browser

Expression: \[qr:(.*)?\] Test string: ert et [qr:4] tertert er Example:

image

Screenshot from playground image

G - modifier must be removed from expression to get string "4" in [1] group in real case.

InSyncWithFoo commented 1 year ago

Seems to be a duplicate of #2031.

working-name commented 1 year ago

@Alexufo https://stackoverflow.com/questions/19913667/javascript-regex-global-match-groups take a look at this

Alexufo commented 1 year ago

@Alexufo https://stackoverflow.com/questions/19913667/javascript-regex-global-match-groups take a look at this

thanks, but I wrote about different behavior site vs any js playground

working-name commented 1 year ago

@Alexufo

The difference lies in the function utilized. It's still javascript, but because javascript is an oddball at times, you get a different return although you use the same regex and flags.

You can still isolate the full match (without the individual groups) from the other functions' return as well if that's all you're after. But if that were the case, then why use a capturing group at all? If you need to group things to make them optional you can use a non-capturing group (?:optional)?.

Hope this helps!


regex101 does not utilize the match function of javascript. Instead you should use either exec or matchAll.

Originally posted by @firasdib in https://github.com/firasdib/Regex101/issues/2031#issuecomment-1494222022