Regex To Match String Between Asterisk With Line Breaks
Example: blah blah * Match this text Match this text Match this text Match this text Match this text * more text more text How to
Solution 1:
You can use a negated match here. Notice that I escaped \
the literal newline for this example.
var myString = "blah blah * Match this text Match this text\
Match this text\
Match this text\
Match this text\
*\
more text more text";
var result = myString.match(/\*([^*]*)\*/);
console.log(result[1]);
// => " Match this text Match this text Match this text Match this text Match this text "
See Working demo
If you don't want the leading or trailing whitespace, you can use the following to make it non greedy.
var result = myString.match(/\*\s*([^*]*?)\s*\*/);
console.log(result[1]);
// => "Match this text Match this text Match this text Match this text Match this text"
Solution 2:
[\s\S]
matches any space and any non-space character. I.e. any character, even line breaks. (tested here).
\*[\s\S]*\*
Solution 3:
Try this: /(\*)([^\0].+)*(\*)/g
:
var regex = /(\*)([^\0].+)*(\*)/g;
var input = "* Match this text Match this text (this is a line break -> \n) Match this text (\n) Match this text Match this text * more text more text";
if(regex.test(input)) {
var matches = input.match(regex);
for(var match in matches) {
alert(matches[match]);
}
}
else {
alert("No matches found!");
}
Solution 4:
These answers will help you both now and in the future.
From the console:
> "blah blah * Match this text Match this text\
Match this text\
Match this text\
Match this text\
*\
more text more text".match(/[*]([^*]*)[*]/)[1]
" Match this text Match this text Match this text Match this text Match this text "
Post a Comment for "Regex To Match String Between Asterisk With Line Breaks"