用JavaScript替换URL的一部分 [英] Replace part of a URL with JavaScript

查看:133
本文介绍了用JavaScript替换URL的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多有关匹配URL,然后替换该特定URL的特定部分的文章...

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

但是!

我需要更改的网站大约有25,000个URL,下面的字符串我需要替换!我想做的是:

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

mysite.com/p/product_name_10011029

然后替换:/p/

使用

mysite.com/product/product_name_10011029

mysite.com/product/product_name_10011029

同样,URL中有大约25,000个不同的页面带有/p/,因此它不能仅匹配一个URL

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

推荐答案

您将javascript和jquery固定为标签: 使用正则表达式替换

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

示例:

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

请提供更多信息,以便我们提供更好的帮助

please provide more informations, so we can help better

这篇关于用JavaScript替换URL的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆