Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using both lookahead and look behind regex

I am having a problem using the following regex. It works fine in regexr and rubular but it gives me an error when running it on node.js. I am fairly new to using regex and i'm not sure what i'm doing wrong. It will work if I knock off the (?<= ) so I presume that is the problem.

I'm trying to match 'is' with a leading and trailing space using /(?<= )is(?= )|==/g

Example with test words:
http://regexr.com?33781

Node error output

 temp = temp.replace(/(?<= )is(?= )|==/g, '===');
^
SyntaxError: Invalid regular expression: /(?<= )is(?= )|==/: Invalid group
at new RegExp (unknown source)
like image 740
SkinnyG33k Avatar asked Dec 21 '12 16:12

SkinnyG33k


People also ask

What is look ahead and look behind in regex?

Lookahead allows to add a condition for “what follows”. Lookbehind is similar, but it looks behind. That is, it allows to match a pattern only if there's something before it.

Can I use regex Lookbehind?

The good news is that you can use lookbehind anywhere in the regex, not only at the start.

Can I use regex lookahead?

Lookahead assertions are part of JavaScript's original regular expression support and are thus supported in all browsers.

What is lookaround in regex?

Lookarounds are zero width assertions. They check for a regex (towards right or left of the current position - based on ahead or behind), succeeds or fails when a match is found (based on if it is positive or negative) and discards the matched portion.


1 Answers

JavaScript regex does not support lookbehind at all.

Sources:

  • http://www.regular-expressions.info/lookaround.html#limitbehindand
  • http://www.regular-expressions.info/javascript.html
  • https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions

However, you can fake it in some cases.

like image 199
Matt Ball Avatar answered Oct 06 '22 18:10

Matt Ball