JavaScript regex multiline flag doesn't work

前端 未结 5 1668
清酒与你
清酒与你 2020-11-22 09:41

I wrote a regex to fetch string from HTML, but it seems the multiline flag doesn\'t work.

This is my pattern and I want to get the text in h1 tag.

5条回答
  •  粉色の甜心
    2020-11-22 10:11

    [\s\S] did not work for me in nodejs 6.11.3. Based on the RegExp documentation, it says to use [^] which does work for me.

    (The dot, the decimal point) matches any single character except line terminators: \n, \r, \u2028 or \u2029.

    Inside a character set, the dot loses its special meaning and matches a literal dot.

    Note that the m multiline flag doesn't change the dot behavior. So to match a pattern across multiple lines, the character set [^] can be used (if you don't mean an old version of IE, of course), it will match any character including newlines.

    For example:

    /This is on line 1[^]*?This is on line 3/m

    where the *? is the non-greedy grab of 0 or more occurrences of [^].

提交回复
热议问题