RegExp flags
Flags change how matching works: g for all matches, i for case-insensitive, and more.
Available since: ES3
Syntax
/pattern/gimsuydExamples
The g flag finds every match, without it only the first:
console.log('a-b-c'.replace(/-/, '+'));
console.log('a-b-c'.replace(/-/g, '+'));Output:
a+b-c
a+b+cThe i flag ignores case:
console.log(/hello/i.test('HELLO there'));Output:
trueThe m flag makes ^ and $ work for each line:
const text = 'one\ntwo\nthree';
console.log(text.match(/^\w+$/gm).join(','));Output:
one,two,threeThe s flag lets the dot match line breaks:
console.log(/a.b/.test('a\nb'), /a.b/s.test('a\nb'));Output:
false trueNotes
Other flags: u handles Unicode and emoji correctly, y matches strictly at lastIndex, d adds group positions to the result, v is an extended Unicode mode. The regex.flags property returns the flags.
See also
- RegExp — A regular expression — a pattern for finding and replacing text.
- String.prototype.matchAll — Returns every match together with its groups.
- String.prototype.replace — Replaces the first match (or all of them with a regex g flag).
In the tutorial
- Regular expressions — Regular expressions in JavaScript for beginners: patterns, flags, character classes, testing, finding and replacing text.