Positive and negative look ahead group # 16

Positive Look ahead group

?= is a positive lookahead, a type of zero-width assertion. Positive lookahead is a pattern used to check the existence of the pattern in the string but not included in the matched result.

Basically, look ahead is only used to match the pattern but not included in the match.

let quit = "abc";
let quRegex = /a(?=b)/;
// if b exists after a, then the matched result will return true.
const result = quRegex.test(quit);
console.log(result);
// Return: true




Negative Look Ahead

?! is a negative look ahead which is used to search in the string with non-existence pattern.

Here, the following example shows that if …

island is absent after long, then the pattern will match. Otherwise the pattern will not match.





Post a Comment

Previous Post Next Post