jQuery将*添加到URL

时间:2011-11-09 13:00:19

标签: javascript jquery

我想在每个关键字后添加*

原始网址:

http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword%22%20extFirstName%3A%22keyword%22%20extemail%3A%22keyword%22%20extworkphone%3A%22keyword%22%20extcellphone%3A%22e%22

,结果应为:

http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword*%22%20extFirstName%3A%22keyword*%22%20extemail%3A%22keyword*%22%20extworkphone%3A%22keyword*%22%20extcellphone%3A%2222keyword*%22

关键字是一个可以更改的动态字词!

4 个答案:

答案 0 :(得分:2)

一个奇怪的请求。我假设这导致在SQL语句中使用*作为通配符来搜索关键字?如果是这样,我会非常仔细地考虑你正在做什么和潜在的安全问题。

var url = "http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword%22%20extFirstName%3A%22keyword%22%20extemail%3A%22keyword%22%20extworkphone%3A%22";
url = url.replace("%22%20", "*%22%20");

答案 1 :(得分:1)

不需要jQuery,只需使用replace

var keyword = 'keyword';
var url = 'http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword%22%20extFirstName%3A%22keyword%22%20extemail%3A%22keyword%22%20extworkphone%3A%22keyword%22%20extcellphone%3A%22e%2'
url = url.replace(keyword, keyword+'*', 'g');

答案 2 :(得分:0)

$('#urlID').replace("%22%20", "*%22%20");

要求您的a-tag上有唯一的ID。

答案 3 :(得分:0)

假设你不确切地知道“关键字”是什么字符串,这将为“%22”(双引号的url编码)所包围的任何内容添加一个星号:

url = url.replace(/%22(\w+)%22/g, "%22$1*%22")

输入:

http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword%22%20extFirstName%3A%22keyword%22%20extemail%3A%22keyword%22%20extworkphone%3A%22keyword%22%20extcellphone%3A%22e%22

输出:

http://w/r/p/peopleresults.aspx?k=Title%3A%22keyword*%22%20extFirstName%3A%22keyword*%22%20extemail%3A%22keyword*%22%20extworkphone%3A%22keyword*%22%20extcellphone%3A%22e*%22