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.
var pattern= /
m = html.search(pattern);
I created a string to test it. When the string contains “\n” the result is always null. If I remove all the “\n” , it gave me the right result, no matter with or without /m flag.
what’s wrong with my regex?
You are looking for the
/.../s modifier, also known as the dotall modifier. It forces the dot
. to also match newlines, which it does not do by default.
The bad news is that it
\s) and its negation (
\S) together, like this:
So in your case the regex would become:
s (dotAll) flag, so in a modern environment your regular expression could be as you wrote it, but with an
s flag at the end (rather than
m changes how
$ work, not
You want the
. with [\s\S] as suggested by @molf.
m (multiline) modifier makes ^ and $ match lines rather than the whole string.
[\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.
/This is on line 1[^]*?This is on line 3/m
where the *? is the non-greedy grab of 0 or more occurrences of [^].
const re = /foo.bar/s; // Or, `const re = new RegExp('foo.bar', 's');`. re.test('foo\nbar'); // → true re.dotAll // → true re.flags // → 's'