I have a string containing an URL, containing /p[num], for example www.test.com/list/p12 (p is the page number used for pagination).
This is my attempt to get the URL without the page number in javascript:
url = url.replace("\\/p\\d+", '');
However, it doesn't replace anything. What am I doing wrong here?
JSFIDDLE: https://jsfiddle.net/t9p95p87/
.replace(). Instead use regex literal.url = url.replace(/\/p\d+/, '')Your way would work if you passed that string to theRegExpconstructor.url = url.replace(new RegExp("\\/p\\d+"), '');window.locationor ana.href, and if the part to remove will always be at the start, you could handle it without a regex by modifying the.pathnameproperty and assigning it back to the same property.