-1

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.

liam xu
  • 2,892
  • 10
  • 42
  • 65
  • Because the dot is inside the character class, and we be matched as a literal dot. Try `

    [\s\S]+?<\/p>` instead. Btw, read https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags

    – The fourth bird Jan 18 '19 at 14:42
  • The reason is that a dot within a character class (`[...]`) looses its "superpowers" - meaning it is only a dot. What you possibly want is `re = /

    [\s\S]+?

    /i;` but please be warned that it is nod advisable to parse `HTML` structures with a regular expression.
    – Jan Jan 18 '19 at 14:42

1 Answers1

0

The reason is that a dot within a character class ([...]) looses its "superpowers" - meaning it is only a dot.
What you possibly want is re = /<p>[\s\S]+?<\/p>/i; but please be warned that it is nod advisable to parse HTML structures with a regular expression:

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);
Jan
  • 42,290
  • 8
  • 54
  • 79