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.


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:

  • hello
  • Hello
  • HELLO
  • HeLLo

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.


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:

  1. The ___ flag makes the regex look for all matches instead of stopping at the first one.
  2. To make ^ and $ detect the start and end of each line, use the ___ flag.
  3. When you want to match text without caring about uppercase or lowercase, use the ___ flag.
  4. The ___ flag helps handle Unicode characters like emojis or non-English text.
  5. When you want a regex that searches only from its current position, use the ___ flag.
  6. The g flag stands for ________, allowing multiple matches across the string.
  7. Without the m flag, ^ and $ only match the start and end of the entire ________.
  8. To make /hello/ match “HELLO”, “Hello”, and “hello”, you’d write it as /hello/___.
  9. The u flag is often called the ________ Flag, because it supports global scripts and symbols.
  10. The y flag is described as ________, because it only matches from its last known position.

<
Previous Post
💡JavaScript Functions Made Easy: From Code to Quiz in Minutes
>
Next Post
JS REGEX 02: Using Regular Expressions (RegEx) for Form Validation in Web Development