JS REGEX 01: Understanding Regex Flags in JavaScript

When working with Regular Expressions (regex) in JavaScript (and other programming languages), flags are optional modifiers that change how a pattern behaves. They can make searches case-insensitive, allow multi-line detection, find all matches at once, and more.
Let’s go through the main regex flags you’ll encounter and understand how each works.
1. The g Flag – Global Search
The g flag stands for global search.
By default, regex stops after the first match. Adding g tells it to continue searching for all occurrences of the pattern in the text.
Example:
/apples/g
This pattern matches every occurrence of “apples” in a string.
Without g, only the first “apples” would be found.
2. The m Flag – Multiline Mode
Normally, regex treats the entire text as a single line, so the anchors:
^matches the start of the whole string, and$matches the end of the whole string.
Adding the m flag changes that behavior. In multiline mode, ^ and $ match the start and end of each line, not just the whole text.
Example:
/^Roses are red$/gm
This matches any line in a multi-line text that exactly says “Roses are red”.
3. The i Flag – Ignore Case
The i flag makes regex case-insensitive, allowing matches regardless of uppercase or lowercase letters.
Example:
/hello/i
This will match:
helloHelloHELLOHeLLo
This flag is useful when you want flexible searches that don’t depend on capitalization.
4. The u Flag – Unicode Support
The u flag enables Unicode matching, allowing regex to properly interpret and work with characters beyond standard ASCII.
This includes emojis, accented letters, and scripts from non-Latin languages.
Example:
/\u{1F600}/u
This matches the 😀 emoji (Unicode U+1F600).
Without the u flag, certain Unicode characters may not be recognized correctly.
5. The y Flag – Sticky Search
The y flag (called the sticky flag) makes regex start matching exactly at the position specified by lastIndex, without skipping ahead.
If there’s no match at that position, the search fails.
This is particularly useful in JavaScript when parsing structured data or performing controlled incremental searches.
Example:
const regex = /word/y;
regex.lastIndex = 5;
regex.exec("some word in the middle");
Here, the regex engine will only check for “word” starting from index 5. It won’t look anywhere else.
⚙️ Summary Table – Regex Flags at a Glance
| Flag | Full Name | Description |
|---|---|---|
g |
Global | Finds all matches, not just the first. |
m |
Multiline | Makes ^ and $ work for each line in multi-line text. |
i |
Ignore Case | Makes the search case-insensitive. |
u |
Unicode | Enables proper matching of Unicode characters (like emojis or accented letters). |
y |
Sticky | Matches only from the current position (lastIndex). |
Understanding and combining these flags can significantly improve how you write and control regex patterns.
🧩 Review Fill-Gap Questions
Fill in the blanks to test your understanding:
- The
___flag makes the regex look for all matches instead of stopping at the first one. - To make
^and$detect the start and end of each line, use the___flag. - When you want to match text without caring about uppercase or lowercase, use the
___flag. - The
___flag helps handle Unicode characters like emojis or non-English text. - When you want a regex that searches only from its current position, use the
___flag. - The
gflag stands for________, allowing multiple matches across the string. - Without the
mflag,^and$only match the start and end of the entire ________. - To make
/hello/match “HELLO”, “Hello”, and “hello”, you’d write it as/hello/___. - The
uflag is often called the ________ Flag, because it supports global scripts and symbols. - The
yflag is described as ________, because it only matches from its last known position.