如何使用 javascript 正则表达式替换 URL 的 主机部分。这可以是带或不带 http 的任何类型的 URL。假设此文本来自 json 文件的内容。
旧文本:
{
"auth" : {
"login" : "http://local.example.com:85/auth/signin",
"resetpass" : "http://local.example.com:85/auth/resetpass",
"profile" : "http://local.example.com/auth/profile"
}
}
期待这样的解决方案:
var NewText = OldText.replace (/(some regex)/g, 'example.com');
获取 NewText 为:
{
"auth" : {
"login" : "http://example.com:85/auth/signin",
"resetpass" : "http://example.com:85/auth/resetpass",
"profile" : "http://example.com/auth/profile"
}
}
我在 这里 找到了相同的内容,但该正则表达式在 javascript 中不起作用。
注意:我正在寻找正则表达式。
原文由 sith 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 URL 函数并设置一个新的主机名: