简体   繁体   中英

Replace part of a URL with JavaScript

I've found lots of articles around matching the URL and then replacing a certain part of just that particular URL...

BUT!

The website I need to change has around 25,000 URLs with the below string I need to replace! What I'm trying to do is:

mysite.com/p/product_name_10011029

And replace: /p/

with

mysite.com/product/product_name_10011029

Again, there's 25,000 or so different pages that have /p/ in the URL so it can't match just one URL

You can use the replace :

url = url.replace('/p/', '/product/');

If you have string, then you can use regex :

myHtml.replace(/mysite\.com\/(p)\/.*/gi, 'product');

You pinned javascript and jquery as tags: use replace with regex

exampe:

string.replace(/mysite\.com\/(.*?)\/(.*)/, 'mysite.com/product/\2');

please provide more informations, so we can help better

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM