JS Regex to match everything inside braces (including nested braces): “{ I want this {and this} and this in one string }”

后端 未结 3 1615
一向
一向 2020-12-10 19:25

Meaning that I simply want to strip the enclosing braces. I can match \"{ this kind of stuff }\" with:

\"{stuff}\".match(/{([^}]*)}/)[1]

Am

相关标签:
3条回答
  • 2020-12-10 19:54

    Regexes are not made for that, and only a few dialects (PCRE and .NET Regex) can cope with recursive structures - JS regex cannot.

    So yes, you're asking for too much. Use a JSON parser instead.

    0 讨论(0)
  • 2020-12-10 20:06

    I agree with Lucero that Regexes are not made for that, but to answer your question:

    The problem is that you match everything except }. But since the inside data you want to match contains itself a }, the content of the outer braces is not matched.

    {
        var foo = {
            bar: 1
    ==> };
    
        var foo2 = {
            bar: 2    
    ==> };
    }
    

    You can use this regex:

    {([\S\s]*)}
    

    To explain:

    {                   match opening {
        (               group
            [\S\s]      match everything, workaround for the missing s option in JS
                  *     greedy, makes it match as much as possible
        )               end group
    }                   match closing }
    
    0 讨论(0)
  • 2020-12-10 20:20

    Try this (I based my code on this answer). It also knows to ignore brackets in strings and comments (single-line and multi-line) - as noted in the comment section of that answer:

    var block = /* code block */
        startIndex = /* index of first bracket */,
        currPos = startIndex,
        openBrackets = 0,
        stillSearching = true,
        waitForChar = false;
    
    while (stillSearching && currPos <= block.length) {
      var currChar = block.charAt(currPos);
    
      if (!waitForChar) {
        switch (currChar) {
          case '{':
            openBrackets++; 
            break;
          case '}':
            openBrackets--;
            break;
          case '"':
          case "'":
            waitForChar = currChar;
            break;
          case '/':
            var nextChar = block.charAt(currPos + 1);
            if (nextChar === '/') {
              waitForChar = '\n';
            } else if (nextChar === '*') {
              waitForChar = '*/';
            }
        }
      } else {
        if (currChar === waitForChar) {
          if (waitForChar === '"' || waitForChar === "'") {
            block.charAt(currPos - 1) !== '\\' && (waitForChar = false);
          } else {
            waitForChar = false;
          }
        } else if (currChar === '*') {
          block.charAt(currPos + 1) === '/' && (waitForChar = false);
        }
      }
    
      currPos++ 
      if (openBrackets === 0) { stillSearching = false; } 
    }
    
    console.log(block.substring(startIndex , currPos)); // contents of the outermost brackets incl. everything inside
    
    0 讨论(0)
提交回复
热议问题