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
220 views
in Technique[技术] by (71.8m points)

JavaScript regex: Positive lookbehind alternative

I am looking for an alternative for this:

(?<=.dd)d

(Match third digit after a period.)

I'm aware I can solve it by using other methods, but I have to use a regular expression and more importantly I have to use replace on the string, without adding a callback.

question from:https://stackoverflow.com/questions/65836063/alternative-positive-look-behind-for-regular-expression-javascript

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

1 Answer

0 votes
by (71.8m points)

Turn the lookbehind in a consuming pattern and use a capturing group:

And use it as shown below:

var s = "some string.005";
var rx = /.dd(d)/;
var m = s.match(/.dd(d)/);
if (m) {
  console.log(m[1]);
}

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

...