[英]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... 我发现了很多有关匹配URL,然后替换该特定URL的特定部分的文章...
BUT! 但!
The website I need to change has around 25,000 URLs with the below string I need to replace! 我需要更改的网站大约有25,000个URL,下面的字符串我需要替换! What I'm trying to do is:
我想做的是:
mysite.com/p/product_name_10011029 mysite.com/p/product_name_10011029
And replace: /p/ 并替换:/ p /
with 与
mysite.com/product/product_name_10011029 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 同样,URL中大约有25,000个不同的页面带有/ p /,因此它不能仅匹配一个URL
You can use the replace
: 您可以使用
replace
:
url = url.replace('/p/', '/product/');
If you have string, then you can use regex
: 如果您有字符串,则可以使用
regex
:
myHtml.replace(/mysite\.com\/(p)\/.*/gi, 'product');
You pinned javascript and jquery as tags: use replace with regex 您将javascript和jquery固定为标签:使用regex替换
exampe: 范例:
string.replace(/mysite\.com\/(.*?)\/(.*)/, 'mysite.com/product/\2');
please provide more informations, so we can help better 请提供更多信息,以便我们更好地提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.