Home > Software design >  Why does the regex dotall flag uses the letter 's' and the sticky flag the letter 'y&
Why does the regex dotall flag uses the letter 's' and the sticky flag the letter 'y&

Time:01-26

What is the historic reason for these flag letters? It's harder to remember them when I can't connect a full word to a flag.

g: Global

i: case Insensitive

m: Multiline

u: Unicode

but

s: dotall

y: sticky

CodePudding user response:

s comes from single line (mode). One of the earlier implementations of regular expressions was provided in Perl (see Wikipedia). See the following description from the Perl documentation on the s modifier:

s

Treat the string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.

Used together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.

The y modifier for "sticky" was introduced by ECMAScript in its 2015 version. We can imagine that "y" was chosen -- referring to the ending letter -- as "s" was no longer available.

  • Related