Lookaround

Lookarounds provide the ability to see if the characters before or after the current position match a certain expression. There are four variants:

  • >>, a positive lookahead. For example, (>> [w]) matches if the position is followed by a word character. That character isn’t included in the match.

  • <<, a positive lookbehind. For example, (<< [w]) matches if the position is directly after a word character.

  • !>>, a negative lookahead. For example (!>> [w]) matches if the position is not followed by a word character. Note that this also matches at the end of the string, so it’s not the same as (>> ![w]), which would require that the position is followed by at least one character.

  • !<<, a negative lookbehind. For example (!<< [w]) matches if the position is not directly after a word character. This also matches at the start of the string, so it’s not the same as (<< ![w]).

Note that lookbehind isn’t supported everywhere, for example in Safari.

Lookaround makes it possible to match a string in multiple ways. For example, (!>> ('_' | 'for' | 'while' | 'if') %) [w]+ % matches a string consisting of word characters, but not one of the keywords _, for, while and if. Be careful when using this technique, because the lookahead might not match the same length as the expression after it. Here, we ensured that both match until the end of the word with %.