2016-12-26 70 views
-2

我有以下字符串:在字符串中的所有网址替换主机 - 的NodeJS

{ 
    "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。我正在寻找使用正则表达式来实现这一点的最佳做法。

我希望使用((?:\d+\.){3}\d+)(?=:\d+)和更换拍摄组会为你工作像

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

不好但很短。 'JSON.parse(JSON.stringify(thatObject).replace(/ someRegex/gi,'myDomain'))' – Tushar

+0

我知道如何进行字符串化。我需要那个正则表达式。 – sith

+0

从谷歌搜索http://www.regextester.com/22 –

回答

1

demo here

+1

工程就像一个魅力。在我的情况下:'var result = data.replace(/((?:\ d +。){3} \ d +)(?=:\ d +)/ g,host);' – sith