Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  10] [ 2]  / answers: 1 / hits: 113469  / 15 Years ago, wed, july 1, 2009, 12:00:00

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= /<div class=box-content-5>.*<h1>([^<]+?)</h1>/mi
m =;
return m[1];

I created a string to test it. When the string contains n, the result is always null. If I removed all the ns, it gave me the right result, no matter with or without the /m flag.

What's wrong with my regex?

More From » 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 does not exist in JavaScript (it does as of ES2018, see below). The good news is that you can work around it by using a character class (e.g. s) and its negation (S) together, like this:


So in your case the regex would become:

/<div class=box-content-5>[sS]*<h1>([^<]+?)</h1>/i

As of ES2018, JavaScript supports the 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; m changes how ^ and $ work, not .):

/<div class=box-content-5>.*<h1>([^<]+?)</h1>/is

[#99209] Friday, June 26, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 181
Total Questions: 99
Total Answers: 96

Location: Argentina
Member since Fri, Oct 21, 2022
2 Years ago
emiliotristinv questions
Wed, Jun 16, 21, 00:00, 3 Years ago
Tue, May 12, 20, 00:00, 4 Years ago
Tue, Mar 31, 20, 00:00, 4 Years ago