Is there a way to define matching brackets? Saying, for example, that if the opening bracket is [then the closing bracket must be a], not} or>
Sort-of.
ERE does not provide a way to match the closing parenthesis with the opening parenthesis as you describe. (It may be possible to use PREG magic, but I will have to leave it to someone else.) You need to either have multiple regular expressions or several atoms within the same regular expression.
If you use one regular expression, then, as I understand it, you will need to determine the type of the found string in brackets, as well as the contents of this string. As mentioned in the comments, you need to do this in your programming language, but you can at least get what you need from the regular expression.
In the expression below, each line style is represented as a "branch" in RE. Branches are divided by or-bars ( | ). For clarity, I assume all lines are [:alnum:] . You did not specify the content, so you will need to adjust your specific requirements.
/(\[)([[:alnum:]]+)\]|(\()([[:alnum:]]+)\)|(\{)([[:alnum:]]+)\}/ β β β β $1 $2 divider divider
Please note that in each branch the first character is enclosed in parentheses, which makes it an βatomβ. You need your code to link to this atom as a backlink. The second atom is the inner string. Now ... my JavaScript is not as strong as my, say, baking skill, but this may be the beginning:
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 that you can combine them into one function or use them differently without making them a function, but you get this idea.