How to capture multiline regex between two tags?
Depending on your underlying engine use the s
-modifier (and add -->
at the end of your expression.
This will make the .
match newline-characters aswell.
If the s
-flag is not available to you, you may use
<!--((?:.*\r?\n?)*)-->
Explanation:
<!-- #start of comment
( #start of capturing group
(?: #start of non-capturing group
.*\r?\n? #match every character including a line-break
)* #end of non-capturing group, repeated between zero and unlimited times
) #end of capturing group
--> #end of comment
To match multiple comment blocks you can use
/(?:<!--((?:.*?\r?\n?)*)-->)+/g
Demo @ Regex101
Regex capture everything between two tags across multiple lines
Javascript does not support multiline expressions when using .
alone. You have to use [\s\S]
in place of .
so an example that satisfies what you want would be:
var x = "<sometag>\n\
123\n\
456\n\
</sometag>";
var ans = x.match(/<sometag>([\s\S]*?)<\/sometag>/im).pop();
// ans equals " 123 456"
note that you still need the m
modifier.
JavaScript regex multiline text between two tags
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:
[\s\S]
So in your case the regex would become:
/<div class="box-content-5">[\s\S]*<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
Regex multiple lines and capture specific text between
You can use
Register(?:[^\r\n]*\r?\n){2}([^\r\n]+(?:\r?\n[^\r\n]+)*)
Details:
Register
- a fixed string(?:[^\r\n]*\r?\n){2}
- two sequences of zero or more chars other than CR and LF and then an optional CR followed with one LF char([^\r\n]+(?:\r?\n[^\r\n]+)*)
- Group 1:[^\r\n]+
- one or more chars other than CRLF(?:\r?\n[^\r\n]+)*
- zero or more repetitions of CLRF or LF line ending sequence and then one or more chars other than CR and LF.
How to match all text between two strings multiline
Well unfortunately, RegExr is dependent on the JS RegExp implementation, which does not support the option to enable the flag/modifier that you need.
You are looking for the s
(DotAll) modifier forcing the dot .
to match newline sequences.
- Live Demo on regular expressions 101
If you are using JavaScript, you can use this workaround:
/<!-- OPTIONAL -->([\S\s]*?)<!-- OPTIONAL END -->/
Related Topics
Youtube Iframe API: How to Control an Iframe Player That's Already in the HTML
How to Access a JavaScript Object Which Has Spaces in the Object's Key
How to Capture an Arbitrary Number of Groups in JavaScript Regexp
How to Get a JavaScript Object's Class
Long Processing Time Likely Due to Getvalue and Cell Inserts
What Does (Function($) {})(Jquery); Mean
Async/Await Always Returns Promise
Variable as the Property Name in a JavaScript Object Literal
How to Compute the Sum and Average of Elements in an Array
Form Serialize JavaScript (No Framework)
Constructor Function VS Factory Functions
Stop Execution of JavaScript Function (Client Side) or Tweak It
Working with Select Using Angularjs's Ng-Options
Node.Js on Multi-Core MAChines
When to Use Vanilla JavaScript VS. Jquery
What Does "Options = Options || {}" Mean in JavaScript
Why Don't We Just Use Element Ids as Identifiers in JavaScript