Flags are options passed to JavaScript Regex, which could be used for special needs. In this article, I will try to show the basic usage of flags.
Use Flag
// in constructor
const re1 = new RegExp("pattern", "flags");
// in inline
const re2 = /pattern/g;
// multiple flag
const re3 = /pattern/gmi;
i
Make case insensitive.
> /p/.test("p")
true
> /p/.test("P")
false
> /p/i.test("P")
true
g
Looks for all matches.
> "aaabaaa".replace(/a/, "");
'aabaaa'
> "aaabaaa".replace(/a/g, "");
'b'
s
Enable dotall mode, which means dot .
could match newline character.
> /.bc/.test("abc")
true
> /.bc/.test("bbc")
true
> /.bc/.test("cbc")
true
> /.bc/.test("\nbc")
false
> /.bc/.test("\rbc")
false
> /.bc/s.test("\nbc")
true
> /.bc/s.test("\rbc")
true
m
Enable multiline mode, ^
and $
not only match the beginning and end of the string, but also at start/end of line.
> let text = `1. line one.
... 2. line two.
... 3. line four.`;
> text.match(/^\d/g);
[ '1' ]
> text.match(/^\d/gm);
[ '1', '2', '3' ]
y
Make the search sticky. Flag y
is similiar to flag g
, but every next match should start exactly from last index.
Let's see an example. If there is no g
or y
flag, exec always return first match.
> let s = "aaba";
> let re = /a/;
> re.exec(s);
[ 'a', index: 0, input: 'aaba', groups: undefined ]
> re.exec(s);
[ 'a', index: 0, input: 'aaba', groups: undefined ]
> re.exec(s);
[ 'a', index: 0, input: 'aaba', groups: undefined ]
If we add flag g
, then every exec should start to search from lastIndex.
> let re = /a/g;
> re.exec(s);
[ 'a', index: 0, input: 'aaba', groups: undefined ]
> re.exec(s);
[ 'a', index: 1, input: 'aaba', groups: undefined ]
> re.exec(s);
[ 'a', index: 3, input: 'aaba', groups: undefined ]
> re.exec(s);
null
But if we use flag y
, every exec should match exactly start from the last index(last index start from 0).
> let re = /a/y;
> re.exec(s);
[ 'a', index: 0, input: 'aaba', groups: undefined ]
> re.exec(s);
[ 'a', index: 1, input: 'aaba', groups: undefined ]
> re.exec(s); // here, last a doesn't match
null
> re.exec("baaba"); // here, first a doesn't match
null
u
Enable full unicode support. The most useful usage I see is try to use \p{...}
to match unicode properties.
Every character in Unicode has a lot of properties. They describe what category the character belongs to. For example, we can use sc
to specify a language. In below example, we try to match japanese katakana.
> let re = /\p{sc=Kana}/u;
> re.test("abc")
false
> re.test("你好")
false
> re.test("こんにちわ")
false
> re.test("リモート")
true