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
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.
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.
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...
var rx = /\[[^\]]+\]|\{[^}]+\}|<[^>]+>/;