Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

regex - Regular expression works on regex101.com, but not on prod

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.

question from:https://stackoverflow.com/questions/65847648/regex-expression-works-on-regex101-but-not-in-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

var re = /(?:s|^)@(S+)/g; 
var str = 's  @vln1
@vln2
';
var res = [];
while ((m = re.exec(str)) !== null) {
  res.push(m[1]);
}
console.log(res);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...