Flags

Flags may be added to a pattern:
g (global): find (and replace) all matches.
i (ignore case): do not differentiate between uppercase letters and lowercase letters.


var s = "aA";
console.log(s.replace(/a/, "x"));
console.log(s.replace(/A/i, "x"));
console.log(s.replace(/a/gi, "x"));

xA xA xx

m (multiline): allow ^ and & to work over lines.


var s = "a\nb\nc";
console.log(s.replace(/^./g, "x"));
console.log(s.replace(/^./gm, "x"));

x b c x x x

u (Unicode): treat pattern as a sequence of Unicode code points.


var s = "Hello你好";
console.log(s.search(/[\u{00FF}-\u{FFFF}]/u));
console.log(s.search(/[\u{00FF}-\u{FFFF}]/));

5 SyntaxError

y (sticky): match from the index indicated by the lastIndex property of the RegExp.


var s = "Hello你好";
var r = /e/y;
r.lastIndex = 0;
console.log(r.test(s));
r.lastIndex = 1;
console.log(r.test(s));
r.lastIndex = 5;
console.log(r.test(s));

false true false

s (space): let the dot . match line terminating characters.


console.log(/./.test("\n"));
console.log(/./s.test("\n"));
console.log(/[^]/s.test("\n"));

false true true

d (has indices), added in ECMAScript 2022: additionally records, for every captured group, the [start,end) index range within the string it matched, available via the match result's .indices array (and .indices.groups for named groups).


const m = /(?<word>\w+)/d.exec('  hello');
console.log(m.indices[0]);
console.log(m.indices.groups.word);

[2, 7] [2, 7]

v (unicodeSets), added in ECMAScript 2024 as an upgrade of u: besides everything u already allows, character classes can now contain set operations — intersection with &&, and subtraction with -- — as well as nested classes and matching of full Unicode 'properties of strings' (eg. whole emoji sequences), not just single code points. u and v are mutually exclusive on the same RegExp.


const asciiWord = /[\p{Alphabetic}--\p{ASCII}]/v;
console.log(asciiWord.test('a'));   // 'a' is ASCII, so subtracted out
console.log(asciiWord.test('é'));   // alphabetic but not ASCII

false true

Inline flag modifiers, added in ECMAScript 2025, let flags be enabled (or, for i, m, s, disabled with a leading -) for just part of a pattern, using the non-capturing-group-like syntax (?flags-flags:...), instead of applying to the whole RegExp.


const r = /Hello (?i:world)!/;
console.log(r.test('Hello world!'));
console.log(r.test('Hello WORLD!'));   // 'i' applies only inside (?i:...)
console.log(r.test('HELLO world!'));   // 'Hello ' outside the group is still case-sensitive

true true false