slevithan / xregexp

Extended JavaScript regular expressions
http://xregexp.com/
MIT License
3.31k stars 278 forks source link

What does (?xn) and (?x) mean in the following? #353

Closed st-clair-clarke closed 1 year ago

st-clair-clarke commented 1 year ago
lib.preexponent = XRegExp.build('(?xn)\
lib.exponent = XRegExp.build('(?x)\....

I saw them at https://blog.stevenlevithan.com/archives/grammatical-patterns-xregexp-build

Thanks

slevithan commented 1 year ago

They are mode modifiers, or an alternative way to provide regex flags as part of the pattern itself. A mode modifier uses the syntax (?imnsuxA), where imnsuxA is any combination of XRegExp flags except g, y, or d (excluded since these latter flags change how various functions apply a regex rather than changing the meaning of the regex pattern itself). XRegExp allows the use of a single mode modifier (?...) at the very beginning of a pattern only. See https://xregexp.com/syntax/#modeModifier for more details.

(?xn) enables two flags: x and n, while (?x) enables just the x flag.

See https://xregexp.com/flags/ for details about these flags.

st-clair-clarke commented 1 year ago

Great. Thank you.