2016-08-11 4 views
0

이 파일에 포함 된 특정 텍스트를 찾으려면 도와주세요. 노드 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

예 그렇습니다. 사이트 2 만 텍스트로 정의합니다. – Dhanan

+0

텍스트로서? – FrankerZ

+0

예. /VirtualHost[\s\S]*?(.*)site2(.*)[s와 비슷한 것을 찾고 있습니다. ? <\/VirtualHost>/gm – Dhanan