Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex match number after string

I have this string

/results?radius=4000&newFilter=true

and I need to replace radius=4000 with radius=n where n is a variable.

How can I use String.replace() method with regex to match that part?

like image 542
nick Avatar asked Apr 16 '26 21:04

nick


1 Answers

You can use /radius=\d+/ to match "radius=" followed by any number of digits. With this we can use the replace() method to replace it with the desired value:

var str = "/results?radius=4000&newFilter=true";
var replacement = 123;

var newStr = str.replace(/radius=\d+/, "radius=" + replacement);

console.log(newStr);
like image 123
James Donnelly Avatar answered Apr 19 '26 11:04

James Donnelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!