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:
gletsexec,matchAll, andreplacework on all matches instead of only the first. - Ignore case:
ilets/hello/imatch"HELLO","Hello", and"hello". - Match line boundaries:
mmakes^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:
uenables Unicode-aware patterns, andyanchors matches tolastIndex.
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");
- Write the pattern you want to match.
- Add
gif you need all matches. - Add
iif case should not matter. - Add
mif you need^and$to work per line. - Use the regex with methods such as
test,exec,matchAll, orreplace.
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:
^hellomatcheshelloat the start of a line.gmakesexeccontinue after each match usinglastIndex.iallows"Hello"and"HELLO"to match.mmakes^match the start of each line.match[0]is the matched text, andmatch.indexis its position in the string.
Common mistakes
- Reusing a global regex object with
test:const re = /hello/g; re.test("hello")can returntrueonce and thenfalsebecauselastIndexmoved. Fix: resetre.lastIndex = 0or use a non-global regex for a simple yes/no check. - Forgetting
mfor line anchors:/^hello/.test("Hi\nhello")isfalsewithoutm. Fix: use/^hello/mwhen you mean the start of a line. - Assuming
isolves all Unicode case issues:ihelps with case-insensitive matching, but Unicode property escapes and full Unicode behavior requireu. Fix: use/\p{L}+/iuwhen you need Unicode-aware matching. - Using
gwithreplacewithout realizing it changes all matches:"aaa".replace(/a/g, "b")gives"bbb", while"aaa".replace(/a/, "b")gives"baa". Fix: choosegonly 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.
| Flag | Use it when | Closest alternative |
|---|---|---|
g | You need every match in the string. | y if you need matches to start exactly at lastIndex. |
i | Case should not affect matching. | Lowercasing both pattern and text for simple ASCII checks. |
m | You need ^ and $ to match line boundaries. | Splitting the text into lines and testing each line separately. |
s | You 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.