Regular expression works on regex101.com, but not on prodRegular expression works on regex101.com, but not on prod - Solution Checker - solutionschecker.com - Find the solution for any programming question. We as a solution checker will focus on finding the fastest possible solution for developers. Main topics like coding, learning.

https://regex101.com/r/sB9wW6/1

(?:(?<=\s)|^)@(\S+) <-- the problem in positive lookbehind

Working like this on prod: (?:\s|^)@(\S+), but I need a correct start index (without space).

Here is in JS:

var regex = new RegExp(/(?:(?<=\s)|^)@(\S+)/g);

Error parsing regular expression: Invalid regular expression: /(?:(?<=\s)|^)@(\S+)/

What am I doing wrong?

UPDATE

Ok, no lookbehind in JS :(

But anyways, I need a regex to get the proper start and end index of my match. Without leading space.

Solution 1

Make sure you always select the right regex engine at regex101.com. See an issue that occurred due to using a JS-only compatible regex with [^] construct in Python.

JS regex - at the time of answering this question - did not support lookbehinds. Now, it becomes more and more adopted after its introduction in ECMAScript 2018. You do not really need it here since you can use capturing groups:

The (?:\s|^)@(\S+) matches a whitespace or the start of string with (?:\s|^), then matches @, and then matches and captures into Group 1 one or more non-whitespace chars with (\S+).

To get the start/end indices, use

BONUS

My regex works at regex101.com, but not in...