2016-12-26 69 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