Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Regular Expression - Modifiers (Flags)

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

2. g (global) - global match

3. m (multiline) - multiline mode

4. s (single line/dotall) - single line mode

5. u (unicode) - Unicode mode

6. y (sticky) - sticky matching

7. x (extended) - extended mode


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

=

"

google

\n

runoob

\n

taobao

"

;

var

n1

=

str

.

match

(

/

google

./

)

;

//

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.