使用jQuery或javascript中的reg表达式将url的开头替换为.com

时间:2015-10-05 18:53:43

标签: javascript jquery regex url

我正在尝试使用reg表达式来替换.com之类的url,例如本地主机。我不是一个有表情的大师,因此有很多麻烦这样做。我们可以为我们的localhost配置Chrome扩展程序,并将所有网址交换为指向本地主机进行调试。所以表达式需要找到类似下面的内容:

*。com / - >并且保持不变,但直到.com与我们的临时字符串值交换。

提前致谢

2 个答案:

答案 0 :(得分:1)

如果你想使用chrome扩展,那么*是有效的

    chrome.webRequest.onBeforeRequest.addListener(
            function(details) {
              details.url = "http://localhost";
            },
            {urls: ["*://*.*.com/*"]}, //could also be urls: ["*://my.host.com/*"]
//also if you want to change all urls you can use ["<all_urls>"]
            ["blocking"]);

答案 1 :(得分:1)

如果您想要使用正则表达式路线,您应该能够完成它:

.*?(?=\.com\b)

https://regex101.com/r/aA5rT3/1

它使用正向前瞻来匹配.com

这里是小提琴:https://jsfiddle.net/520zdnsL/