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.


RESETRUNFULL
<!DOCTYPE html><html><body><script>


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

</script></body><html>

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


RESETRUNFULL
<!DOCTYPE html><html><body><script>


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

</script></body><html>

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


RESETRUNFULL
<!DOCTYPE html><html><body><script>

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

</script></body><html>

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


RESETRUNFULL
<!DOCTYPE html><html><body><script>


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

</script></body><html>

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


RESETRUNFULL
<!DOCTYPE html><html><body><script>

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

</script></body><html>

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