Regular Expression - Modifiers (Flags)
Regex modifiers (also called pattern modifiers or flags) are special directives used to change the matching behavior of a regular expression.
Flags are also called modifiers; regex flags are used to specify additional matching strategies.
Flags are not written inside the regex; they are placed outside the expression, in the following format:
/pattern/flags
Common modifiers
The following table lists the commonly used regex modifiers:
1. i (ignore case) - ignore case
-
Make matching case-insensitive
-
Example:
/abc/iCan match "abc", "Abc", "ABC", etc. -
Supported languages: almost all regex implementations (JavaScript, PHP, Python, etc.)
2. g (global) - global match
-
Find all matches instead of stopping after the first match
-
Example: in the string "ababab",
/ab/gwill match all three "ab" -
Supported languages: JavaScript, PHP, etc.
3. m (multiline) - multiline mode
-
changing
^and$alters its behavior so that it matches the start and end of each line, not just the start and end of the entire string -
Example: in a multiline string,
/^abc/mwill match "abc" at the start of each line -
Supported languages: JavaScript, PHP, Python, Perl, etc.
4. s (single line/dotall) - single line mode
-
makes the dot
.match all characters including newlines -
is called "dotall" mode in JavaScript, using
/sModifiers -
Example:
/a.b/scan match "a\nb" -
Supported languages: PHP, Perl, Python (as
re.DOTALL), JavaScript(ES2018+)
5. u (unicode) - Unicode mode
-
Enable full Unicode support
-
Correctly handle UTF-16 surrogate pairs and Unicode character properties
-
Example:
/\p{Script=Greek}/uCan match Greek letters -
Supported languages: JavaScript, PHP, etc.
6. y (sticky) - sticky matching
-
Start matching from the current position of the target string (using
lastIndexproperty) -
Similar to
^anchor, but targets the starting position of the match -
Example: in JavaScript,
/a/ywill start fromlastIndexto match "a" -
Supported languages: JavaScript
7. x (extended) - extended mode
-
Ignore whitespace and comments in the pattern, making the regex more readable
-
Example: in PHP,
/a b c/xis equivalent to/abc/ -
Supported languages: PHP, Perl, Python (as
re.VERBOSE)
g modifier
The g modifier can find all matches in a string:

Example
Find "runoob" in a string:
var
str
=
"
Google runoob taobao runoob
"
;
var
n1
=
str
.
match
(
/
runoob
/
)
;
//
Find the first match
var
n2
=
str
.
match
(
/
runoob
/
g
)
;
//
Find all matches
Try it »
i modifier
The i modifier enables case-insensitive matching, as shown below:

Example
Find "runoob" in a string:
var
str
=
"
Google runoob taobao RUNoob
"
;
var
n1
=
str
.
match
(
/
runoob
/
g
)
;
//
Case Sensitivity
var
n2
=
str
.
match
(
/
runoob
/
gi
)
;
//
Case-insensitive
Try it »
m modifier
The m modifier makes ^ and $ match the start and end positions of each line in a block of text.
g matches only the first line; adding m enables multiline matching.

The following example uses \n to break lines:
Example
Find "runoob" in a string:
var
str
=
"
runoobgoogle
\n
taobao
\n
runoobweibo
"
;
var
n1
=
str
.
match
(
/^
runoob
/
g
)
;
//
Match a
var
n2
=
str
.
match
(
/^
runoob
/
gm
)
;
//
Multiline match
Try it »
s modifier
By default, the dot . matches any character except the newline \n; after adding s, . also matches the newline \n.

The s modifier example is as follows:
Example
Find in a string:
var
str
=
"
\n
runoob
\n
taobao
"
;
var
n1
=
str
.
match
(
/
./
)
;
//
Without s, cannot match \n
var
n2
=
str
.
match
(
/
runoob
./
s
)
;
//
With s, matches \n
Try it »
Additional notes
Table of language-specific modifier supplements:
| Language | Specific modifiers | Description |
|---|---|---|
| PHP | A |
Anchor the pattern to the start of the string |
D |
$Match only the end of the string (excluding the trailing newline) |
|
U |
Reverse the greediness of quantifiers (make all quantifiers non-greedy) | |
| Python | re.A |
Make\w,\W,\b,\Betc. match ASCII characters only |
re.L |
determine based on locale settings\w,\Wthe meaning of , etc. |
|
| JS(ES2022) | d |
Generate an indices property for match results (containing the start and end indexes of the match positions) |
Table of modifier combination examples:
| Combination | effect |
|---|---|
gi |
Global match + ignore case (e.g., find the "email" word in all formats) |
ims |
Ignore case + multiline mode + dot matches newline (commonly used in log analysis) |
gu |
Global match + Unicode support (e.g., find all Unicode emoji) |
Inline modifier table (PCRE/Perl style):
| Syntax | Scope | Example |
|---|---|---|
(?i) |
Enable ignore case | a(?i)bc -> matches "aBc", "aBC" |
(?-i) |
Disable ignore case | a(?i)b(?-i)c -> matches only "aBc" |
(?i:...) |
Apply only within parentheses | a(?i:b)c -> matches "aBc", "abc" |
Note: implementations of modifiers may differ across languages; it is recommended to refer to the documentation of the specific language when using them.