0
0

More than 3 years have passed since last update.

Javascript Regular Expression Memo

Last updated at Posted at 2020-01-11

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.

-\/]

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0