如何使用JavaScript附加到锚点的href属性但不影响查询字符串?

我有一些链接(它们在一个数组中,而不是一个元素)…

> 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返回引用,但它对我不起作用:(

相关文章

kindeditor4.x代码高亮功能默认使用的是prettify插件,prett...
这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代...
js如何实现弹出form提交表单?(图文+视频)
js怎么获取复选框选中的值
js如何实现倒计时跳转页面
如何用js控制图片放大缩小