Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regular expression URL alteration

I have a URL like /admin/editblogentry?page=3&color=blue

I want to alter the 'page' in the url to 1 so that the url becomes

/admin/editblogentry?page=1&color=blue

What is the best way to accomplish this using javascript?

like image 560
smahesh Avatar asked Apr 28 '26 14:04

smahesh


2 Answers

var s="/admin/editblogentry?page=3&color=blue"
var re=/(.*page=)(\d+)(&.*)*/
s.replace(re,"$11$3")
like image 156
ghostdog74 Avatar answered Apr 30 '26 03:04

ghostdog74


Assuming that the URL contains only one number (i.e. the page numbers), this is the simplest regex:

"/admin/editblogentry?page=3&color=blue".replace(/\d+/, 10001)
like image 24
polygenelubricants Avatar answered Apr 30 '26 05:04

polygenelubricants