mewi mewi - 2 months ago 14
Javascript Question

JavaScript: What's the difference between a positive look ahead and a non-capturing group?

If I get it right then a positive look ahead ( ?= ) as well a non-capturing group ( ?: ) are used for to excluse a part of the string in the final match-results.

What is the difference between the a lookahead and a non-capturing group?

Can anyone explain? Preferable with an easy understandable example?

Answer

A positive look ahead is a zero width assertion. This means that it will not consume an input, e.g. in

/(?=.)./

the (?=.) part will match with the first characters of the string and the . will match with the same character. On the other hand

/(?:.)./

the (?:.) part will match with the first characters of the string (just like before) but the . will match with the second character because the first character will have been consumed by the parentheses.

Comments