Jammy Lee
Jammy Lee

Reputation: 1516

What is the different between /[^\n\S]*:(?!:)/ and /[^\n\S]*:/ in javascript

I was reading the source code of lex of coffeescript, and I got below regex for IDENTIFIER

IDENTIFIER = /^([$A-Za-z_\x7f-\uffff][$\w\x7f-\uffff]*)([^\n\S]*:(?!:))?/;

Just not sure why (?!:) is required though I know it is a non-capturing negative lookahead group

Upvotes: 0

Views: 34

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074238

It requires that the match not be followed by a second :. Without it, the match could be followed by a second :. So with that negative lookahead, given the input foo::, only foo matches; without it there, given the input foo::, foo: (with the colon) matches. You can play with it over on regex101.

Upvotes: 1

Related Questions