javascript regular express flag table
Flag | Description |
---|---|
g | Global search. |
i | Case-insensitive search. |
m | Multi-line search. |
s | Allows . to match newline characters. (Added in ES2018, not yet supported in Firefox). |
u | "unicode"; treat a pattern as a sequence of unicode code points. |
y | Perform a "sticky" search that matches starting at the current position in the target string. |
Sticky Flag
Bellow is sticky. Regexp holds lastIndex, and when reads strings, lastIndex
is added. When test failed, lastIndex
is set to 0.
const str1 = 'table football';
const regex1 = new RegExp('foo','y');
regex1.lastIndex = 6;
console.log(regex1.test(str1));
// expected output: true
console.log(regex1.lastIndex);
// expected output: 9
regex1.test("table foofoo");
// expected true;
console.log(regex1.lastIndex)
// expected output: 12
regex1.test("table foofoo");
// expected false;
console.log(regex1.lastIndex)
// expected output: 0
$& means matched string
"01234".replace(/[0-9]/g, 'v$&')
// expected output: "v0v1v2v3v4"
Back slash \
before normal character in string has no meaning
"\a" means "a"
"\t" means "\t"(tab)
Escaped Regexp Function
function escapeRe(value) {
return new RegExp(value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'm');
}
Special Characters
normal context
-\/^$*+?.()|[]{}
In [] context.
-\/]