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>