I'm trying, in javascript, to get a regexp to match areas contained by curly braces (and including) in a string where there are one or more line breaks, and possible tag nesting. So it would match {the quick brown fox ... lazy dog}
inside the string:
did you know {the quick brown fox
jumps
over the lazy dog} is a pangram?
but not the curly braced area inside the following string, as it doesn't contain a line break
did you know {the quick brown fox jumps over the lazy dog} is a pangram?
I'm ing unstuck when there is nesting, because I want only brown \n fox
to match in the following example, since it is the immediate brace containing the line break.
did you know {the quick {brown
fox} jumps over the lazy dog} is
a pangram?
the obvious /\{([^{]+)\}/g
will match anything inside a curly set, but that can mean it can erroneously match on {the quick {brown \n fox}
and {brown \n fox} jumps over the lazy dog}
. I'm starting to think that substring matching might be the way, something like the way a css template system like mustache js does it.
I'm trying, in javascript, to get a regexp to match areas contained by curly braces (and including) in a string where there are one or more line breaks, and possible tag nesting. So it would match {the quick brown fox ... lazy dog}
inside the string:
did you know {the quick brown fox
jumps
over the lazy dog} is a pangram?
but not the curly braced area inside the following string, as it doesn't contain a line break
did you know {the quick brown fox jumps over the lazy dog} is a pangram?
I'm ing unstuck when there is nesting, because I want only brown \n fox
to match in the following example, since it is the immediate brace containing the line break.
did you know {the quick {brown
fox} jumps over the lazy dog} is
a pangram?
the obvious /\{([^{]+)\}/g
will match anything inside a curly set, but that can mean it can erroneously match on {the quick {brown \n fox}
and {brown \n fox} jumps over the lazy dog}
. I'm starting to think that substring matching might be the way, something like the way a css template system like mustache js does it.
6 Answers
Reset to default 5To ensure there are at least one newline char in the string, use the regex like below:
/\{(?=.*\n)[^}]+\}/
console:
> "aaa{ccc}bbb".match(/\{(?=.*\n)[^}]+\}/)
null
> "aaa{ccc\ndddd}bbb".match(/\{(?=.*\n)[^}]+\}/)
["{ccc
dddd}"]
This pattern will work /\{(?=.*\s)[^{}]+\}/
From console
a = "aa{aaaa
bb}b"
b = "aaaa{cccc}bbb"
c = "aaaaaaa{sssss{ddd
dd}bbb}sss"
a.match(/\{(?=.*\s)[^{}]+\}/)
["{aaaa
bb}"]
b.match(/\{(?=.*\s)[^{}]+\}/)
null
c.match(/\{(?=.*\s)[^{}]+\}/)
["{ddd
dd}"]
try this one:
\{([^{\n]+)\}
I tested it here: http://regex101./r/vJ6gU1
I think this is what you're after:
/\{[^{}\n]*\n[^{}]*\}/
[^{}\n]*\n
consumes everything up to the next linefeed, but it fails if it sees a brace first.
Then [^{}]*
consumes the rest because it doesn't matter if there are any more linefeeds.
I'm assuming your line separators consist of linefeed alone (\n
) or carriage return plus linefeed (\r\n
). There are some other possibilities, but they're very rare.
This one works for your string:
"did you know {the quick {brown\nfox} jumps over the lazy dog} is\na pangram?"
.match(/\{[^{}]+\}/)
Returns {brown\nfox}
.
/\{[^{}]+\}/
:
- Match a
{
. - Match everything that's not a
{
or}
. - Match a
}
DEMO
This will work Try it
\{([^{]+?)\}