title: Regular expression to match brackets
body: <p>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 &lt;tag&gt; or [element].</p>
  
  <p>Currently my regular expression looks like this:</p>
  
  <pre><code>var rx=/([\[\{&lt;])([\s\S]+?)([\]\}&gt;])]/;
  </code></pre>
  
  <p>The issue is that such a regular expression doesn't force brackets to match. For example in the following string:</p>
  
  <pre><code>[{lastName},{firstName}]
  </code></pre>
  
  <p>the regular expression will match <code>[{lastName}</code></p>
  
  <p>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 ></p>