6

my multiple string is something like below

###
some content
that I need to match
and might have some special character in it such as | <> []
###

I am trying to get the content of between hashes. I have tried below regex but it is not matching to it and return null.

var regex = /### ((.|\n)*) ###/;
var match= regex.exec(aboveContentAsString);
console.log(match[1]);
rawData
  • 729
  • 4
  • 10
  • 17
  • 2
    take out the spaces in the regex – Les Mar 05 '15 at 23:27
  • `### (` tries to match `###` followed by a space. However in your example text, `###` is followed by a line break, so no match. Why do you have the spaces in the expression? – Felix Kling Mar 05 '15 at 23:28

3 Answers3

19

JavaScript lacks the s (singleline/dotall) regex option, but you can workaround it by replacing . with [\s\S] (match any character that is a whitespace or that is not a whitespace, which basically means match everything). Also, make your quantifier lazy and get rid of the spaces in the pattern, since there's also no x (extended) option in JS:

var regex = /###([\s\S]*?)###/;

Example:

var input = "###\nsome content\nthat I need to match\nand might have some special character in it such as | <> []\n###";

var regex = /###([\s\S]*?)###/;
document.getElementById("output").innerText = regex.exec(input)[1];
<pre id="output"></pre>

Your original approach could have worked, but I'd add \r in there too:

var regex = /###((?:.|[\r\n])*?)###/;

But I prefer the [\s\S] approach since it's shorter and (IMHO) more readable.

Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
  • 2
    Sidenode: Can be ANY distinct pair, like `[\d\D]`, `[\w\W]`, or even `[^]` (**all but nothing**) :-) – dognose Mar 05 '15 at 23:44
  • 1
    @dognose beware with `[^]` - I reccomend against it since it "works" only in JS and is not standard regex syntax: in other flavors, the `]` will be interpreted as `\]` and the character class will be unterminated (syntax error). – Lucas Trzesniewski Mar 05 '15 at 23:47
  • Youre right. Regex Hint #25566 recorded :P – dognose Mar 05 '15 at 23:51
0

Javascript has no Dot-Matches-All-Option - therefore just use: dot OR whitespace. (first \s* to make sure your match does not start with a linebreak - not mandatory ofc.)

^###\s*((?:.|\s)*)###$

Regular expression visualization

Debuggex Demo

dognose
  • 20,360
  • 9
  • 61
  • 107
0

If you would like the regex not to match the three hashes, try something like this:
(?![###])[^#]((.|\s)[^#])*. This uses a negative lookbehind to start matching only when it cannot match ###. It starts matching and continues until it hits another hash.

Blue0500
  • 715
  • 8
  • 16