How to make javascript regular expression match any char and carriage return?

时间:2019-01-18 14:38:59

标签: javascript regex

I have a html file like below.

  <body>  
    <p>Enter your phone number with area code and then click 
        Check The expected format is like</p>
    <form action="#">  
      <input id="phone"><button onclick="testInfo(document.getElementById('phone'));">Check</button>
    </form>  
  </body>  
</html>

I use below script which can print right result.

fs.readFile('test.html', 'utf8', function (err, data) {
    var re = /<p>[a-zA-Z1-9\s\r\n]*<\/p>/i;
    var myArray = data.match(re);
    console.log(myArray);
});

the result: '<p>Enter your phone number with area code and then click \r\n Check The expected format is like</p>'

But I want to use regular expresstion like below.

re = /<p>[.\r\n]*<\/p>/i;

But it print null. How to fix it? Why I can not use . to replace a-zA-Z1-9\s thanks.

1 个答案:

答案 0 :(得分:0)

原因是字符类([...])中的点失去了其“超能力”,这意味着它只是一个点。
您可能想要的是re = /<p>[\s\S]+?<\/p>/i;,但请注意,建议不要使用正则表达式来解析HTML结构:

let html = ` <body>  
    <p>Enter your phone number with area code and then click 
        Check The expected format is like</p>
    <form action="#">  
      <input id="phone"><button onclick="testInfo(document.getElementById('phone'));">Check</button>
    </form>  
  </body>  
</html>`;

re = /<p>[\S\s]+?<\/p>/i;
var myArray = html.match(re);
console.log(myArray);