Regular Expressions β Flags (Modifiers) |
Regular Expressions - Flags (Modifiers)
Regular expression flags (also called pattern modifiers or flags) are special instructions used to alter the matching behavior of a regular expression.
Flags are also known as modifiers. The flags of a regular expression are used to specify additional matching strategies.
Flags are not written inside the regular expression; they are placed outside the expression, in the following format:
/pattern/flags
Common Flags
The following table lists common flags used with regular expressions:
1. i (ignore case) - Case-Insensitive Matching
- Makes the match case-insensitive.
- Example:
/abc/ican match "abc", "Abc", "ABC", etc. - Supported in: Almost all regular expression implementations (JavaScript, PHP, Python, etc.)
2. g (global) - Global Matching
- Finds all matches, rather than stopping after the first match.
- Example: In the string "ababab",
/ab/gwill match all three "ab"s. - Supported in: JavaScript, PHP, etc.
3. m (multiline) - Multiline Mode
- Changes the behavior of
^and$to match 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 in: JavaScript, PHP, Python, Perl, etc.
4. s (single line/dotall) - Single Line Mode
- Makes the dot
.match all characters, including newline characters. - In JavaScript, this is called "dotall" mode, using the
/sflag. - Example:
/a.b/scan match "anb". - Supported in: PHP, Perl, Python (as
re.DOTALL), JavaScript (ES2018+).
5. u (unicode) - Unicode Mode
- Enables full Unicode support.
- Correctly handles UTF-16 surrogate pairs and Unicode character properties.
- Example:
/p{Script=Greek}/ucan match Greek letters. - Supported in: JavaScript, PHP, etc.
6. y (sticky) - Sticky Matching
- Starts matching from the current position in the target string (using the
lastIndexproperty). - Similar to the
^anchor, but for the starting position of the match. - Example: In JavaScript,
/a/ywill match "a" starting fromlastIndex. - Supported in: JavaScript.
7. x (extended) - Extended Mode
- Ignores whitespace and comments in the pattern, making the regular expression more readable.
- Example: In PHP,
/a b c/xis equivalent to/abc/. - Supported in: PHP, Perl, Python (as
re.VERBOSE).
The g Flag
The g flag can find all matches in a string:
Example
Find "" in a string:
var str="Google taobao ";
var n1=str.match(//);
var n2=str.match(//g);
The i Flag
The i flag performs a case-insensitive match. Example:
Example
Find "" in a string:
var str="Google taobao ";
var n1=str.match(//g);
var n2=str.match(//gi);
The m Flag
The m flag allows ^ and $ to match the start and end positions of each line in a block of text.
g only matches the first line; adding m enables multiline matching.
The following example uses n for line breaks in the string:
Example
Find "" in a string:
var str="tutorialgooglen taobaon tutorialweibo";
var n1=str.match(/^/g);
var n2=str.match(/^/gm);
The s Flag
By default, the dot . matches any character except the newline character n. With the s flag, . includes the newline character n.
Example of the s flag:
Example
Find in a string:
var str="googlen tutorialn taobao";
var n1=str.match(/google./);
var n2=str.match(/./s);
Extended Explanation
Language-Specific Flags Supplement Table:
| Language | Specific Flag | Description |
|---|---|---|
| PHP | A |
Anchors the pattern to the start of the string. |
D |
$ matches only the end of the string (not the end of a line with a trailing newline). |
|
U |
Inverts the greediness of quantifiers (makes all quantifiers non-greedy). | |
| Python | re.A |
Makes w, W, b, B, etc., match only ASCII characters. |
re.L |
Determines the meaning of w, W, etc., based on locale settings. |
|
| JS (ES2022) | d |
Generates an indices property for the match result (containing start and end indices of the match). |
Flag Combination Examples Table:
| Combination | Effect |
|---|---|
gi |
Global matching + case-insensitive (e.g., finding all forms of the word "email"). |
ims |
Case-insensitive + multiline mode + dot matches newline (often used in log analysis). |
gu |
Global matching + Unicode support (e.g., finding all Unicode emojis). |
Inline Flags Table (PCRE/Perl Style):
| Syntax | Scope | Example |
|---|---|---|
(?i) |
Enables case-insensitive matching. | a(?i)bc β matches "aBc", "aBC" |
(?-i) |
Disables case-insensitive matching. | a(?i)b(?-i)c β only matches "aBc" |
(?i:...) |
Applies only within the parentheses. | a(?i:b)c β matches "aBc", "abc" |
Note: Implementations of flags may vary across different languages. It is recommended to refer to the specific language's documentation when using them.
YouTip