Regular Expressions17. Lookahead |
|
Join the DiscussionMore of this FeatureIntroduction Pattern Matching Ignore Case and Global Replacing Matches Spliting Matches Start and End Meta Characters Character Classes Repetitions Greedy, Reluctant, etc. Predefined Classes Special Characters ASCII and Unicode Boundaries Grouping Back References Non-Capturing Groups Lookahead means being able to examine what follows a particular pattern within our regular expression without actually including that following text in the pattern itself. There are two variants of lookahead processing that allow us to either test for the following text matching a particular pattern or alternatively for the following text to not match a particular pattern. Lookaheads allow you to test what comes next in the text string without changing the current position being tested within the text. In addition, lookaheads are not captured in the backreference array.
var re = /\b(.+(?=work\b))/;
This regular expression will look for words that end in "work" and will capture the prefix in the backreference array. For example "homework" would save "home", "schoolwork" would save "school" etc.
var re = /\b(c\w+(?!ment))/;
This regular expression will look for words that start with "c" and do not contain "ment". This expression would therefore match "contain" and "create" for example and these words would be added to the backreference array. It would not match such words as "commenting" or "containment" and so these words would not be added to the backreference array. |

