在字符串-Nodejs中替换所有URL中的主机



我有以下字符串:

{
   "auth" : {
     "login" : "http://123.123.11.22:85/auth/signin",
     "resetpass" : "http://123.123.22.33:85/auth/resetpass",
     "profile" : "http://123.123.33.44:85/auth/profile"
   }
}

我需要用我的主机名替换所有IP地址以获取以下输出:

{
   "auth" : {
      "login" : "http://mydomain:85/auth/signin",
      "resetpass" : "http://mydomain:85/auth/resetpass",
      "profile" : "http://mydomain:85/auth/profile"
   }
}

我可以将此字符串转换为对象,通过属性进行迭代,拆分并重新加入形成URL。我正在寻找最佳的实践,以实现这一目标。

我期望

var newUrl = text.replace( /someRegex/gi, 'mydomain');

使用((?:d+.){3}d+)(?=:d+)并更换捕获的组将为您工作。

演示在这里

最新更新