Regular expression to match brackets

后端 未结 4 1359
清酒与你
清酒与你 2021-01-04 11:37

For a templating engine, I am using regular expressions to identify content under brackets in a string. For example the regex needs to match {key} or or [element

相关标签:
4条回答
  • 2021-01-04 12:06

    Is there a way to define matching brackets? Saying for example that if the opening bracket is a [ then the closing bracket must be a ], not a } or a >

    Sort-of.

    ERE does not provide a way for you to match a closing bracket to an opening bracket the way you describe. (It may be possible using PREG magic, but I'll have to leave that for someone else.) You'll need either to have multiple regular expressions, or multiple atoms within a single regular expression.

    If you use a single regex, I gather you'll need to determine the type of bracketed string you're detecting, as well as the content of that string. As was mentioned in comments, you'll need to do this in your programming language, but you can at least get what you need out of the regex.

    In the regex below, each style of string is represented as a "branch" in the RE. Branches are separated by or-bars (|). For clarity, I'm assuming all strings are [:alnum:]. You haven't specified content, so you'll need to adjust for your particular requirements.

    /(\[)([[:alnum:]]+)\]|(\()([[:alnum:]]+)\)|(\{)([[:alnum:]]+)\}/
     ↑   ↑               ↑                    ↑
     $1  $2           divider              divider
    

    Note that in each branch, the first character is enclosed by round brackets, making it an "atom". You need your code to refer to this atom like a backreference. The second atom is the inner string. Now ... my JavaScript isn't as strong as my, say, baking skill, but this might be a start:

    String.prototype.bracketstyle = function() {
      var re = /(\[)([:alnum:]+)\]|(\()([:alnum:]+)\)|(\{)([:alnum:]+)\}/;
      return this.replace(re,"$1");
    }
    
    String.prototype.innerstring = function() {
      var re = /(\[)([:alnum:]+)\]|(\()([:alnum:]+)\)|(\{)([:alnum:]+)\}/;
      return this.replace(re,"$2");
    } 
    

    I suspect you could combine these into a single function, or use them differently without making them a function, but you get the idea.

    0 讨论(0)
  • 2021-01-04 12:17

    The best way to do this, especially if different brackets can have different meanings, is to split into 3 regular expressions:

    var rx1 = /\[([^\]]+)]/;
    var rx2 = /\(([^)]+)\)/;
    var rx3 = /{([^}]+)}/;
    

    These will match any text surrounded by [], (), and {} respectively, with the text inside in the first matched group.

    0 讨论(0)
  • 2021-01-04 12:19

    you could use alternatives using pipe character (|) like this one /\[([\s\S]+?)\]|\{([\s\S]+?)\}|<([\s\S]+?)>/, although it gets pretty long.

    EDIT: shortend the regex, is not that long any more...

    0 讨论(0)
  • 2021-01-04 12:20
    var rx = /\[[^\]]+\]|\{[^}]+\}|<[^>]+>/;
    
    0 讨论(0)
提交回复
热议问题