Im trying to use javascript and regex to replace a substring in a url.
myurl.com/?page_id=2&paged=2
shall become
myurl.com/?page_id=2&paged=3
this is my code that doesnt seem to work:
nextLink = 'myurl.com/?page_id=2&paged=2'
nextLink = nextLink.replace(/\/paged\=\/[0-9]?/, 'paged='+ pageNum);
What am i doing wrong here? Im new to regex.
You’re telling it to match
/paged, but there’s no/pagedin your string. Also,[0-9]?probably isn’t what you want for the digits. Try this:That tells it to replace
&pageid=...(where...is a series of one or more digits) with the given string.