繁体   English   中英

JavaScript字符串替换URL参数

[英]JavaScript string replace URL parameter

我目前正在尝试更改帮助链接。 帮助手册的网址中有一个pageid。 当您浏览我们的应用程序时,链接和pageid会更改。

网址的示例是: http : //www.google.com/custom? pageid=# pageid

目前我正在使用

function ReplaceHelpLink(pageId) {
    $(".helpLinkReplace", document).each(function (index, helpLink) {
         helpLink.href = helpLink.href.replace("#pageid", pageId);
    });
 }

但这并不能解决这种情况,因为网址已更改,例如http://www.google.com/custom?pageid=1

您将如何处理? 感谢您的帮助和时间。

function ReplaceHelpLink(pageId) {
    $(".helpLinkReplace", document).each(function (index, helpLink) {
         var link = $(this).attr("href").replace("#pageid", pageId);
         $(this).attr("href",link);
    });
 }

您需要替换以下内容:

....replace(/#pageid|\d+/,pageId);

这将同时处理第一次替换和后续替换。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM