假设我有以下网址;
http://www.domain.com/en-US/assortment/en-US/category/page.aspx?sub=GROUP7
正如您所看到的,我在网址中有两个语言层,我只想删除第二个语言层。所以我希望网址是这样的。
http://www.domain.com/en-US/assortment/category/page.aspx?sub=GROUP7
由于技术限制,我没有任何其他方法来修改网址。这可以用jQuery或Javascript实现吗?如果是,怎么样?
答案 0 :(得分:2)
'your url here...'.replace( '/assortment/en-US/category/', '/assortment/category/');
答案 1 :(得分:1)
查看JavaScript replace函数。
好吧,使用替换功能,你可以做一些丑陋的事情:
var url= "http://www.domain.com/en-US/assortment/en-US/category/page.aspx?sub=GROUP7";
var index = url.lastIndexOf('en-US'); //get last index of language
var substr = url.substr(index); //get substring of the tail
var newsubstr = substr.replace('en-US',''); //use replace to get rid of second lang
var newurl = url.substr(0,index-1); //first part of the url
var cleanurl = newurl + newsubstr; //concatenate it
alert(cleanurl);
我没有花时间让它变得更干净 - 我会在几分钟内清理它。
答案 2 :(得分:0)
因为您指的是语言代码,所以我会编写一个javascript代码来捕获通用语言代码并替换复制。
您可以使用正则表达式轻松完成此操作。看看这个
url = "http://www.domain.com/en-US/assortment/en-US/category/page.aspx?sub=GROUP7"
//define regular expression to catch /en-US block
// '/' in front of the language code is also catch in this regular expression
rxp = /(\/[\w]{2}(-[\w]{2})?)/
//split by regular expression
ary = url.split(rxp)
//set the fourth eliment to "" the 4th eliment is the second '/en-US'
ary[3] = ""
//join array with "" to have the prepare the string
url_new = ary.join("")
join方法中的参数是必不可少的。
这应该适用于当前使用的大多数语言代码。