Quick Start
Tutorial
Search & Replace
Tools & Languages
Examples
Reference
Regex Reference
Introduction
Table of Contents
Quick Reference
Characters
Basic Features
Character Classes
Shorthands
Anchors
Word Boundaries
Quantifiers
Capturing Groups & Backreferences
Named Groups & Backreferences
Special Groups
Unicode Characters and Properties
Unicode Versions
Unicode Categories
Unicode Scripts
Unicode Blocks
Unicode Binary Properties
Unicode Property Sets
Unicode Boundaries
Mode Modifiers
Recursion & Balancing Groups
Backtracking Control Verbs
Replacement Reference
Characters
Matched Text & Backreferences
Case Conversion
Context
Conditionals
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference
Book Reviews
Printable PDF
About This Site
RSS Feed & Blog
RegexBuddy—Better than a regular expression reference!

Regular Expression Reference: Mode Modifiers

Mode modifier syntax consists of two elements that differ among regex flavors. Parentheses and a question mark are used to add the modifier to the regex. Depending on its position in the regex and the regex flavor it may affect the whole regex or part of it. If a flavor supports at least one modifier syntax, then it will also support one or more letters that can be used inside the modifier to toggle specific modes. If it doesn’t then “n/a” is indicated for all letters for that flavor.

If a flavor supports more than one modifier syntax then it supports the same options for all syntax, perhaps with the exception of -, x, and the locale settings which may not be supported in the middle of the regex. So the table below has one entry for each letter that applies to all modifier syntax, but separate entries for - and x depending on the modifier syntax.

If a flavor supports mode modifiers but does not support a particular letter then that letter is indicated as “no”. That does not mean that the flavor doesn’t have this mode at all. The flavor may still have the mode, but no option to turn it off. Modes are also not necessarily off by default. For example, in most regex flavors, ^ and $ match at the start and end of the string only by default. But the Just Great Software applications and Ruby, they match at the start and end of each line by default. In the JGsoft applications, you can turn off this mode with (?-m) while in Ruby you cannot turn off this mode at all. (?-m) affects the dot rather than the anchors in Ruby.

The table below only indicates whether each flavor supports a particular letter to toggle a particular mode. It does not indicate the defaults.

FeatureSyntaxDescriptionExampleJGsoft Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE PCRE2 PHP Delphi R Ruby std::regex Boost Tcl POSIX GNU Oracle XML XPath
Mode modifier (?letters) at the start of the regex A mode modifier at the start of the regex affects the whole regex and overrides any options set outside the regex. (?i)a matches a and A. YESYESnonoYESYESYESYESdefaultYESYESYESYESYESYESYESnoECMAYESnonononono
Mode modifier (?letters) in the middle of the regex A mode modifier in the middle of the regex affects the whole regex and overrides any options set outside the regex. te(?i)st matches test, teST, TEst and TEST. no2.4–3.6nononononononononononononononononononononono
Mode modifier (?letters) in the middle of the regex A mode modifier in the middle of the regex affects only the part of the regex to the right of the modifier. If the modifier is used inside a group, it only affects the part of the regex inside that group to the right of the modifier. If the regex or group uses alternation, all alternatives to the right of the modifier are affected. te(?i)st matches test and teST but not TEst or TEST. YESnonononoYESYESYESdefaultYESYESYESYESYESYESYESnoECMAnononononono
Mode modifier (?letters) followed by alternation A mode modifier in the middle of the regex affects alternatives to the right of the modifier up until the end of the group that contains the modifier or until the end of the regex. te(?i)st|two matches TWO. YESn/an/an/an/aYESYESYESdefaultYESYESYESYESYESYES1.9n/aECMAn/an/an/an/an/an/a
Modifier group (?letters:regex) Non-capturing group with modifiers that affect only the part of the regex inside the group. te(?i:st) matches test and teST but not TEst or TEST. YES3.6YESnoYESYESYESYESdefaultYESYESYESYESYESYESYESnoECMAnononononono
Negative modifier (?on-off) at the start of the regex Modifier letters (if any) before the hyphen are turned on, while modifier letters after the hyphen are turned off. (?-i)test matches test but not Test. YESnon/an/anoYESYESYESdefaultYESYESYESYESYESYESYESn/aECMAnon/an/an/an/an/a
Negative modifier (?on-off) in the middle of the regex and/or (?on-off:regex) Modifier letters (if any) before the hyphen are turned on, while modifier letters after the hyphen are turned off. (?i)te(?-i)st matches test and TEst but not teST or TEST. YES3.6YESn/aYESYESYESYESdefaultYESYESYESYESYESYESYESn/aECMAn/an/an/an/an/an/a
Reset modifiers (?^) Turn off all options. The caret can be followed by modifier letters to turn some options back on. (?i)te(?^)st matches test and TEst but not teST or TEST. nononon/anononononoYESno10.327.3.0no4.0.0non/anonon/an/an/an/an/a
Free-spacing (?x) at the start of the regex Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments. (?x)a#b matches a YESYESn/an/aYESYESYESYESnoYESYESYESYESYESYESYESn/aECMAYESn/an/an/an/an/a
Free-spacing (?x) in the middle of the regex or a group (?x:regex) Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments after previously ignoring whitespace. (?x:a#b ) matches a YES3.6non/anoYESYESYESnoYESYESYESYESYESYESYESn/aECMAn/an/an/an/an/an/a
Exact spacing (?-x) in the middle of the regex or a group (?-x:regex) Turn off free-spacing to make whitespace significant after previously ignoring it. (?-x:a #b) matches a #b YES3.6non/anoYESYESYESnoYES7.0YESYESYESYESYESn/aECMA
1.42
n/an/an/an/an/an/a
Exact spacing (?t) at the start of the regex Turn on “tight” or exact spacing mode to treat whitespace and # characters as literals. (?t)a#b matches a#b nonon/an/anonononononononononononon/anoYESn/an/an/an/an/a
Free-spacing (?xx) Turn on free-spacing mode to ignore whitespace between regex tokens and allow # comments, both inside and outside character classes. (?xx)[ a] matches a but not   nonoerrorn/anonononodefault error5.26
5.22 error
no10.307.3.0no4.0.0non/anonon/an/an/an/an/a
Case insensitive (?i) Turn on case insensitivity. (?i)a matches a and A. YESYESYESn/aYESYESYESYESdefaultYESYESYESYESYESYESYESn/aECMAYESn/an/an/an/an/a
Case sensitive (?c) Turn on case sensitivity. (?c)a matches a but not A. nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Single-line (?s) Make the dot match all characters including line break characters. (?s).* matches ab\n\ndef in ab\n\ndef YESYESYESn/aYESYESYESYESdefaultYESYESYESYESYESYESnon/aECMAnon/an/an/an/an/a
Multi-line (?m) Make ^ and $ match at the start and end of each line. (?m)^. matches a and d in ab\n\ndef YESYESYESn/aYESYESYESYESdefaultYESYESYESYESYESYESnon/aECMAnon/an/an/an/an/a
Single-line (?m) Make the dot match all characters including line break characters. (?m).* matches ab\n\ndef in ab\n\ndef nononon/anononononononononononoYESn/anonon/an/an/an/an/a
Tcl single-line (?s) Make ^ and $ match at the start and end of the string only. Make the dot match all characters including line break characters. (?s)^.{3} matches only ab\n in ab\n\ndef nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Tcl multi-line (?m) Make ^ and $ match at the start and end of each line. Do not allow the dot and negated character classes to match line break characters. (?m)^. matches a and d in ab\n\ndef nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Tcl multi-line (?n) Make ^ and $ match at the start and end of each line. Do not allow the dot and negated character classes to match line break characters. (?n)^. matches a and d in ab\n\ndef nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Tcl “partial” newline-sensitive (?p) Make ^ and $ match at the start and end of the string only. Do not allow the dot and negated character classes to match line break characters. (?p)^.* matches only ab in ab\n\ndef nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Tcl “weird” newline-sensitive (?w) Make ^ and $ match at the start and end of each line. Make the dot match all characters including line break characters. (?w)^. matches a, the first \n, and d in ab\n\ndef nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Explicit capture (?n) Plain parentheses are non-capturing groups instead of numbered capturing groups. Only named capturing groups actually capture. (?n)(a|b)c is the same as (?:a|b)c YESnonon/aYESYESnonono5.22no10.307.3.0no4.0.0non/anonon/an/an/an/an/a
Duplicate named groups (?J) Allow multiple named capturing groups to share the same name. (?J)(?:(?'x'a)|(?'x'b))\k'x' matches aa or bb nononon/anononononono6.7YESYESYESYESnon/anonon/an/an/an/an/a
Ungreedy quantifiers (?U) Switches the syntax for greedy and lazy quantifiers. Its use is strongly discouraged because it confuses the meaning of the standard quantifier syntax. (?U)a* is lazy and (?U)a*? is greedy nononon/anonononodefaultnoYESYESYESYESYESnon/anonon/an/an/an/an/a
UNIX lines (?d) When anchors match at line breaks and when the dot does not match line breaks, make them recognize only the line feed character as a line break. (?dm)^. matches a and c in a\rb\nc nononon/anonoYESYESnonononononononon/anonon/an/an/an/an/a
UAX 29 word boundaries (?w) Word boundaries match according to Unicode Standard Annex 29 instead of matching between a word and non-word character. (?w)\b.\b matches ! because the WB1 and WB2 rules in UAX 29 treat the start and end of the string as a word boundary. (?-w)\b fails to match because the string ! has no word characters. nononon/anononoYESnonononononononon/anonon/an/an/an/an/a
BRE (?b) Interpret the regular expression as a POSIX BRE. (?b)a\+ matches aaa nononon/anonononononononononononon/anoYESn/an/an/an/an/a
ERE (?e) Interpret the regular expression as a POSIX ERE (?e)[a\]+ matches a\a\a nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Literal (?q) Interpret the regular expression as a literal string (excluding the modifier). (?q)[a\]+ matches [a\]+ literally. nononon/anonononononononononononon/anoYESn/an/an/an/an/a
Extra syntax (?X) Treat letters that are escaped with a backslash and that don’t form a regex token as an error instead of as a literal. (?X)\q is an error while (?-X)\q matches q nononon/anonononononoYESno5.5.0–7.2.34YES2.14.0–3.6.3non/anonon/an/an/an/an/a
ASCII classes (?a) Shorthand classes and/or POSIX classes match only ASCII characters. (?a)\d matches only ASCII digits 0 to 9 no3.0non/anononononoYESno10.43nono4.4.02.0n/anonon/an/an/an/an/a
ASCII case folding (?a) Only ASCII characters are matched case insensitively when the regex is case insensitive. (?ai) matches but not no3.0non/anonononononononononononon/anonon/an/an/an/an/a
Unicode classes (?u) Shorthand classes and/or POSIX classes match Unicode characters. (?u)\d matches noYESnon/anononononoYESnonononono2.0n/anonon/an/an/an/an/a
Unicode case folding (?u) All characters are matched case insensitively when the regex is case insensitive. (?ui) matches no2.5non/anonoYESnononononononononon/anonon/an/an/an/an/a
Case insensitive (?u) Turn on case insensitivity. (?ui) matches nononon/anono5 onlynononononononononon/anonon/an/an/an/an/a
Unicode classes and case folding (?U) Shorthand classes and/or POSIX classes match Unicode characters. Implies (?u) to turn on Unicode case folding. (?U)\dȗ matches ๑Ȗ nononon/anono7nononononononononon/anonon/an/an/an/an/a
Separate ASCII/Unicode case folding (?aa) All characters are matched case insensitively when the regex is case insensitive, but ASCII characters cannot match non-ASCII characters or vice versa. (?aai)\x{212A} matches the Kelvin symbol but not the letter k nononon/anononononoYESnonononononon/anonon/an/an/an/an/a
Separate ASCII/Unicode case folding (?r) All characters are matched case insensitively when the regex is case insensitive, but ASCII characters cannot match non-ASCII characters or vice versa. (?aai)\x{212A} matches the Kelvin symbol but not the letter k nononon/anonononononono10.43nono4.4.0non/anonon/an/an/an/an/a
Default classes and case folding (?d) Shorthand and POSIX classes and case folding are reset to their defaults. nononon/anononononoYESnonononono2.0n/anonon/an/an/an/an/a
Locale settings (?l) Shorthand and POSIX classes and case folding use the active locale. nononon/anononononoYESnonononononon/anonon/an/an/an/an/a
Locale settings (?L) Shorthand and POSIX classes and case folding use the active locale. no2.4–3.5non/anonononononononononononon/anonon/an/an/an/an/a
Locale settings Mode modifiers that change shorthand and POSIX classes and/or case folding can be used in the middle of the regex. nononon/anonoYESnonoYESno10.43nono4.4.02.0n/anon/an/an/an/an/an/a
FeatureSyntaxDescriptionExampleJGsoft Python JavaScript VBScript XRegExp .NET Java ICU RE2 Perl PCRE PCRE2 PHP Delphi R Ruby std::regex Boost Tcl POSIX GNU Oracle XML XPath