Skip to content

JavaScript Tutorial Regular Expressions

JavaScript Regex Flags

Lesson 97 of 258 2 min read By Deepak Paswan Updated Oct 4, 2026

Flags go after the closing slash and change how the whole pattern behaves.

script.js
JS
/pattern/gi
FlagNameEffect
gglobalfind all matches, not just the first
iignore casea matches A
mmultiline^ and $ match at each line
sdotAll. also matches line breaks
uunicodeproper handling of Unicode characters
ystickymatch only at the exact lastIndex
dindicesinclude start/end positions of matches

g: all matches

script.js
JS
const text = "cat, Cat, cAt";

console.log(text.match(/cat/));    // first match only (with details)
console.log(text.match(/cat/g));   // → ["cat"]
console.log(text.match(/cat/gi));  // → ["cat", "Cat", "cAt"]

i: ignore case

script.js
JS
console.log(/hello/i.test("HELLO")); // → true
console.log(/hello/.test("HELLO"));  // → false

m: multiline

script.js
JS
const lines = "one\ntwo\nthree";

console.log(lines.match(/^\w+$/g));   // → null (^ and $ only match the whole text)
console.log(lines.match(/^\w+$/gm));  // → ["one", "two", "three"]

u: unicode

Without u, an emoji counts as two characters in a pattern. Use u when you work with non-English text or emoji.

script.js
JS
console.log("😀".length);                 // → 2
console.log(/^.$/.test("😀"));            // → false
console.log(/^.$/u.test("😀"));           // → true

The lastIndex gotcha with g

A regex that has the g (or y) flag remembers where it stopped, in its lastIndex property. Calling test() repeatedly on the same regex can give surprising results.

script.js
JS
const re = /a/g;

console.log(re.test("a")); // → true
console.log(re.test("a")); // → false   (it continued after the first match!)

If you use test() for validation, do not use the g flag. Or create the regex fresh each time.

Combining flags

The order does not matter: /x/gi and /x/ig are the same.

script.js
JS
const re = new RegExp("hello", "gi");
console.log(re.flags);  // → gi