0

Question: how to match any word that is not preceded by .?

In the context of a code syntax highlighter, unlike: Negative lookbehind equivalent in JavaScript, I don't want to reverse string then combine with lookahead nor reverse the matches order (like $1$0) since I concat multiple regex with |, nor use XRegExp, nor use string delimiters ^, $, ..

With PCRE I would simply do: /(?<!\.)\b[a-z]\w+/gi

As I keep forgetting how to simply solve this out I would like to know if there is a recurrent pattern I can remember for mimicking negative look-behinds in JavaScript. Using look-ahead and reversing the matching logic always gets me dizzy!

Also most answers in SO use string delimiters, so it's not the same difficulty.

This is the closest I got: /((?!\.) )\b[a-z]\w+/, https://regex101.com/r/9hw2Y2/1, but it matches a one length char before my match (can see in green with PCRE flavor selected).

halfer
  • 19,824
  • 17
  • 99
  • 186
antoni
  • 5,001
  • 1
  • 35
  • 44
  • @wiktor, it is not aduplicate, one answer suggests reversing string, 1 has a known width match nothing similar with my conditions here, please help! – antoni Apr 07 '17 at 08:59
  • Why reverse when you need an optional group? See [the accepted answer](http://stackoverflow.com/a/641432/3832970). – Wiktor Stribiżew Apr 07 '17 at 09:01
  • my regex is much longer than just that it looks more like `/("(?:\\"|[^"])*")|('(?:\\'|[^'])*')|(\/\/.*|\/\*[\s\S]*?\*\/)|(<[^>]*>)|(!==?|(?:[\[\](){}.:;,+\-?=]|<|>)+|&&|\|\|)|\b(\d+|null)\b|\b(true|false)\b|\b(new|getElementsBy(?:Tag|Class|)Name|getElementById|arguments|if|else|do|return|case|default|function|typeof|undefined|instanceof|this|document|window|while|for|switch|in|break|continue|length|var|(?:clear|set)(?:Timeout|Interval))(?=\W)|(?!\.)([a-zA-Z][\w\d_]*)|(?:(?=\W))(\$|jQuery)(?=\W|$)/g` and I want to append this bit to my current regexp. So i can't just reverse the matches – antoni Apr 07 '17 at 09:05
  • Parsing *code* with *regex*? You are still bound to use the solution I linked to. Reversing a string to use a lookbehind is not really the most efficient way to do it. Adding the optional group and checking it in the code is much cleaner. You cannot use a lookbehind anyway, it is not supported, and there is no similar regex construct in JS regex. – Wiktor Stribiżew Apr 07 '17 at 09:08
  • It doesn't help me, I edited my question please reopen or give me a solution! SO purpose is to help each other... – antoni Apr 07 '17 at 09:24
  • The answer is simple: **It is *NOT* possible with plain JS regex**. And the [accepted answer](http://stackoverflow.com/a/641432/3832970) does not even hint at string reversing. – Wiktor Stribiżew Apr 07 '17 at 09:25

0 Answers0