文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. Code
    2. 2.2. Negative lookahead
    3. 2.3. Global Flag
  3. 3. Reference

Defined

Get the question when looking up for templates engine realization.
And this article mainly focus RegExp.test() and (?!) Negative lookahead condition.

Main()

Code

  • And the explanation

Negative lookahead

Symbol: A(?!B)
Function: Match A whose word behide it is NOT B.

Global Flag

Mainly talking about g flag.

  1. withoutG VS withG2
    test function will save the pattern match session, so the 2nd withG search will return false;
    and global will return back to index 0 to repeat check the str, 3rd is true, of course;
  2. withG, str, repeatStr
    in 2nd loop, withG’s output is not like the withG2 though they are same RegExp, because of the tmpStr reset withG’s match string, so withG’s result always get true even it has global flag.

PS: Same as RegExp.exec()

Reference

test | MDN
regexr

文章目录
  1. 1. Defined
  2. 2. Main()
    1. 2.1. Code
    2. 2.2. Negative lookahead
    3. 2.3. Global Flag
  3. 3. Reference