Back to JavaScript Notes
Topic #341

RegExp Flags

You will be able to choose JavaScript RegExp flags such as g, i, and m to control whether a pattern matches all occurrences, ignores case, and treats line boundaries correctly.

What it is

A JavaScript regular expression has two parts: the pattern and the flags. The pattern describes the text shape you want to find. The flags are lowercase letters written after the closing slash, such as /hello/i, and they change how the engine scans the input. The most common flags are g for global matching, i for ignore-case matching, and m for multiline matching. Related terms include lastIndex, which tracks where a global or sticky search resumes, and flags such as s (dotAll), u (Unicode), and y (sticky).

Why it matters

  • Find every match: g lets exec, matchAll, and replace work on all matches instead of only the first.
  • Ignore case: i lets /hello/i match "HELLO", "Hello", and "hello".
  • Match line boundaries: m makes ^ and $ match the start and end of each line, not just the whole string.
  • Write shorter code: flags replace manual loops, lowercasing, or splitting when the regex engine can do the work.
  • Control advanced behavior: u enables Unicode-aware patterns, and y anchors matches to lastIndex.

Syntax or steps

The smallest useful pattern is:

/pattern/flags

Flags can appear in any order, but they must be valid lowercase letters. You can also build a regex dynamically:

const re = new RegExp("hello", "gim");
  1. Write the pattern you want to match.
  2. Add g if you need all matches.
  3. Add i if case should not matter.
  4. Add m if you need ^ and $ to work per line.
  5. Use the regex with methods such as test, exec, matchAll, or replace.

Example

const text = "Hello\nHELLO\nhello";
const re = /^hello/gim;

let match;
while ((match = re.exec(text)) !== null) {
  console.log(match[0], match.index);
}

Expected output:

Hello 0
HELLO 6
hello 12

Part by part:

  • ^hello matches hello at the start of a line.
  • g makes exec continue after each match using lastIndex.
  • i allows "Hello" and "HELLO" to match.
  • m makes ^ match the start of each line.
  • match[0] is the matched text, and match.index is its position in the string.

Common mistakes

  • Reusing a global regex object with test: const re = /hello/g; re.test("hello") can return true once and then false because lastIndex moved. Fix: reset re.lastIndex = 0 or use a non-global regex for a simple yes/no check.
  • Forgetting m for line anchors: /^hello/.test("Hi\nhello") is false without m. Fix: use /^hello/m when you mean the start of a line.
  • Assuming i solves all Unicode case issues: i helps with case-insensitive matching, but Unicode property escapes and full Unicode behavior require u. Fix: use /\p{L}+/iu when you need Unicode-aware matching.
  • Using g with replace without realizing it changes all matches: "aaa".replace(/a/g, "b") gives "bbb", while "aaa".replace(/a/, "b") gives "baa". Fix: choose g only when replacing every match is intended.

When to use it

Flags are usually better than manual string processing, but the right flag depends on the job.

FlagUse it whenClosest alternative
gYou need every match in the string.y if you need matches to start exactly at lastIndex.
iCase should not affect matching.Lowercasing both pattern and text for simple ASCII checks.
mYou need ^ and $ to match line boundaries.Splitting the text into lines and testing each line separately.
sYou want . to match newlines.Using [\s\S] to match any character.

Practice

Guided exercise: Write a regex that finds every occurrence of cat in "Cat cat CAT", ignoring case. Use matchAll or a loop with exec.

const re = /cat/gi;
console.log([... "Cat cat CAT".matchAll(re)].map(m => m[0]));

Expected output: ["Cat", "cat", "CAT"].

Challenge: Predict the result of running const re = /hello/g; re.test("hello") twice in a row. Hint: check what lastIndex does after the first successful match.

Quick check

Question: Which flag makes ^ and $ match the start and end of each line?

Answer: The m flag.

Summary

RegExp flags are small modifiers that change the matching strategy: g scans all matches, i ignores case, and m treats line boundaries specially. Choosing the right flags keeps regex code shorter and more predictable, especially when combined with methods like exec, test, and replace.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

RegExp Flags – FAQs

Quick answers about learning RegExp Flags in JavaScript.

This free note from Coding Now Tech Institute explains RegExp Flags in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including RegExp Flags, is 100% free with no signup required.
With focused practice, most students grasp RegExp Flags in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now