正则表达式匹配完全相同的块

时间:2016-08-11 17:45:27

标签: javascript regex node.js

请帮我查看此文件中包含的特定文本块。 我正在使用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--" );

上面的代码工作正常,但它将匹配所有vhost块。 我只需要找到包含&#34; www.site2.com&#34;

的vhost块

1 个答案:

答案 0 :(得分:1)

经过一番商议,我得出的结论是,正则表达式不会削减芥末。我建议使用node-apacheconf

public class ContactController : ApiController
    {
        IContactService service;

        // dependency injection is being done here by Unity. look in UnityConfig where we register IContactSevice to ContactService. Whenever asp.net see's IContactService it now knows to make a new ContactService instance
        // we do this for 2 reasons: 1) this makes unit testing possible where we can mock the IContactService and 2) this makes maintaining the code easier where we can change the contact service class we want to use later and we wouldn't have to change the code here in this controller
        public ContactController(IContactService _service)
        {
            service = _service;
        }