2016-08-11 38 views
0

請幫我找出這個文件中包含的特定文本塊。 我使用Node js fs閱讀上下文。正則表達式以匹配完全相同的塊

<VirtualHost *:80> 
DocumentRoot /home/site1 
ServerName www.site1.com 
</VirtualHost> 
<VirtualHost *:80> 
DocumentRoot /home/site2 
ServerName www.site2.com 
</VirtualHost> 
<VirtualHost *:80> 
DocumentRoot /home/site3 
ServerName www.site3.com 
</VirtualHost> 

代碼:

fileContext.toString().split("\n"); 
var matched = fileContext.toString().replace(/<VirtualHost[\s\S]*?<\/VirtualHost>/gm,"--matched--"); 

上面的代碼工作正常,但它會匹配所有虛擬主機塊。 我只需要找到包含「www.site2.com」的虛擬主機塊

回答

1

經過一番商議,我得出結論,正則表達式不會削減芥末。我建議使用node-apacheconf

var name = "www.site2.com"; 

apacheconf('/etc/apache2/httpd.conf', function(err, config, parser) { 
    if (err) throw err 

    console.log(config.VirtualHost.filter(function(vh) { 
    return vh.ServerName == name; 
    })); 
}); 
+0

是的這是,我不能定義只有site2作爲文本 – Dhanan

+0

作爲一個文本?你必須逃避它,如果你把它放到正則表達式 – FrankerZ

+0

是的即時尋求類似於//gm – Dhanan