Skip to content

/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

bash
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]);
EOF

Output

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

FlagNameEffect
gglobalfind all matches; needed for matchAll and replaceAll with a regex
iignoreCasecase-insensitive
mmultiline^ and $ match at line breaks, not only at the start and end of the string
sdotAll. also matches line terminators
uunicodecode points instead of UTF-16 units, \p{…} property escapes, stricter syntax
vunicodeSetseverything u does plus set operations ([A--B], [A&&B]) and string properties
ystickymatch only at lastIndex
dhasIndicesadd start and end indices of each group to the result

Sources