我有一些链接(它们在一个数组中,而不是一个元素)…
> http://example.com
> http://example.com?bob=true
> http://example.com?sandy=true\u0026amp;bob=false
我想在.com之后但在查询字符串开始之前附加一些内容.
例
> http://example.com/search/results
> http://example.com/search/results?bob=true
> http://example.com/search/results?sandy=true\u0026amp;bob=false
什么是最好的方法?
更新
我回答了我自己的问题,但如果我的解决方案可以改进(或者你可以看到一些问题),请发表你自己的答案.
解决方法
我想出了这个……
var append = '/search/results'; anchor.href = anchor.href.replace(/(\?|$)/,append + '$1');
我以为我可以省略括号,然后用$0返回引用,但它对我不起作用:(