最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript regex to match braces with line breaks, but not without - Stack Overflow

programmeradmin6浏览0评论

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.

Share Improve this question asked Feb 28, 2014 at 7:31 frumbertfrumbert 2,4275 gold badges34 silver badges62 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 5

To 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}.

/\{[^{}]+\}/:

  1. Match a {.
  2. Match everything that's not a { or }.
  3. Match a }

DEMO

This will work Try it

\{([^{]+?)\}
发布评论

评论列表(0)

  1. 暂无评论