/flags/regex/javascript-regex-flags
JavaScript regex flags: g, i, m, s, u, v, y and d
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · grep (GNU grep) 3.0updated 2026-10-20
- tool
- Regex
- command
- /pattern/flags or new RegExp(pattern, flags)
- flag
- g i m s u v y d
- default
- none
Flags go after the closing slash, /pattern/gi, or as the second argument of new RegExp(pattern, "gi"). They change how the whole pattern matches; JavaScript has no inline flags such as (?i) for the whole pattern.
Each flag, run in Node.js
node - <<'EOF'
console.log("node", process.version);
const show = (label, value) => console.log(label.padEnd(30), JSON.stringify(value));
show("no flag 'a1 b2'.match(/\\d/)", "a1 b2".match(/\d/)[0]);
show("g match(/\\d/g)", "a1 b2".match(/\d/g));
show("i /hello/i.test('HeLLo')", /hello/i.test("HeLLo"));
show("m 'a\\nb'.match(/^b/m)", "a\nb".match(/^b/m)?.[0]);
show("(no m) 'a\\nb'.match(/^b/)", "a\nb".match(/^b/));
show("s /a.b/s.test('a\\nb')", /a.b/s.test("a\nb"));
show("(no s) /a.b/.test('a\\nb')", /a.b/.test("a\nb"));
show("u '😀'.match(/./u)", "😀".match(/./u)[0]);
show("(no u) '😀'.match(/./)[0].length", "😀".match(/./)[0].length);
show("u /\\p{L}+/u on 'café'", "café".match(/\p{L}+/u)[0]);
show("v /[\\p{L}--[a-z]]/v on 'aÉb'", "aÉb".match(/[\p{L}--[a-z]]/v)[0]);
const y = /\d/y; y.lastIndex = 1;
show("y /\\d/y at lastIndex 1 of 'a1'", y.exec("a1")?.[0]);
show("d /b+/d indices on 'abbc'", /b+/d.exec("abbc").indices[0]);
EOFOutput
node v24.20.0
no flag 'a1 b2'.match(/\d/) "1"
g match(/\d/g) ["1","2"]
i /hello/i.test('HeLLo') true
m 'a\nb'.match(/^b/m) "b"
(no m) 'a\nb'.match(/^b/) null
s /a.b/s.test('a\nb') true
(no s) /a.b/.test('a\nb') false
u '😀'.match(/./u) "😀"
(no u) '😀'.match(/./)[0].length 1
u /\p{L}+/u on 'café' "café"
v /[\p{L}--[a-z]]/v on 'aÉb' "É"
y /\d/y at lastIndex 1 of 'a1' "1"
d /b+/d indices on 'abbc' [1,3]Summary
| Flag | Name | Effect |
|---|---|---|
| g | global | find all matches; needed for matchAll and replaceAll with a regex |
| i | ignoreCase | case-insensitive |
| m | multiline | ^ and $ match at line breaks, not only at the start and end of the string |
| s | dotAll | . also matches line terminators |
| u | unicode | code points instead of UTF-16 units, \p{…} property escapes, stricter syntax |
| v | unicodeSets | everything u does plus set operations ([A--B], [A&&B]) and string properties |
| y | sticky | match only at lastIndex |
| d | hasIndices | add start and end indices of each group to the result |
Sources
Related pages
cheat sheets · regexRegex cheat sheet: characters, quantifiers, groups and lookaroundsRegex cheat sheet: character classes, anchors, quantifiers, greedy and lazy matching, groups and backreferences, lookarounds, with tested Python examples.compat · regexRegex features by engine: JavaScript, Python, PCRE and POSIX EREWhich regex features work where: lookbehind, named groups, \p{L}, atomic groups and possessive quantifiers in JavaScript, Python re, PCRE and POSIX ERE, tested.glossary · regexGreedy vs lazy quantifiers in regexA greedy quantifier such as .* matches as much as it can and backtracks only when needed; a lazy one such as .*? matches as little as possible. Examples.