DWQA QuestionsCategory: ProgramHow does a regular expression achieve a match but does not include such a requirement
Scoundrel asked 1 month ago

There is a string like “mid = 61266817536127044185 & srcid = 3002299072”
How to write a regular expression if you want to match the first number after mid =? I used this new regexp (/ mid = ([0-9] +) / g); I found “mid = 6126681753”, but I didn’t want “mid =”.

3 Answers
Best Answer
felix021 answered 1 month ago

Another method is PCRE’s look ahead. If you are interested, you can learn about it.For an example, see the comments in this response.

lanisle replied 1 month ago

var matches = new RegExp(/(?!mid=)[0-9]+/g).exec(“?mid=6126681753,6127044185&srcid=3002299072”); console.log(matches[0])

lanisle answered 1 month ago

Now that you’ve specified a subpattern, that’s fine.

var matches = new RegExp(/mid=([0-9]+)/g).exec("?mid=6126681753,6127044185&srcid=3002299072");
AA Guoliang answered 1 month ago
var g = '';
var s ="?mid=6126681753,6127044185&srcid=3002299072";
var r = new RegExp(/mid=([0-9]+)/g);
var rr = s.replace(r,function($1,$2){g=$2;return $2});

You can simplify yourself….