简体   繁体   中英

How to get all content between two curly brackets, ignoring those in quotation marks?

I'm currently using the following regex with partial success:

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

But it fails for this situation:

{get('{')} {b}

I just want to ignore the { and } when they are between quotation marks, and get the right result:

get('{')

and

b

You may use this regex to capture inner strings for both cases:

\{((?:[^}']*')*[^}']*)\}

Assuming quotes are balanced and unescaped.

RegEx Demo

RegEx Details:

  • \{ : Match a {
  • ( : Start capture group #1
    • (?: : Start a non-capture group
      • [^}']* : Match 0 or more characters that are not } and not '
      • ' : Match a single quote
    • )* : End non-capture group. Match 0 or more of this group
    • [^}']* :
  • ) : End capture group #1
  • \} : Match a }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM