What do `?i` and `?-i` in regex mean?

(?i) starts case-insensitive mode

(?-i) turns off case-insensitive mode

More information at the “Turning Modes On and Off for Only Part of The Regular Expression” section of this page:

Modern regex flavors allow you to apply modifiers to only part of the
regular expression. If you insert the modifier (?ism) in the middle of
the regex, the modifier only applies to the part of the regex to the
right of the modifier. You can turn off modes by preceding them with a
minus sign. All modes after the minus sign will be turned off. E.g.
(?i-sm) turns on case insensitivity, and turns off both single-line
mode and multi-line mode.

Not all regex flavors support this. JavaScript and Python apply all
mode modifiers to the entire regular expression. They don’t support
the (?-ismx) syntax, since turning off an option is pointless when
mode modifiers apply to the whole regular expressions. All options are
off by default.

You can quickly test how the regex flavor you’re using handles mode
modifiers. The regex (?i)te(?-i)st should match test and TEst, but not
teST or TEST.

Leave a Comment