Regular expression to match brackets

For the pattern engine, I use regular expressions to identify the contents in brackets in a string. For example, the regular expression must match {key} or <tag> or [element].

Currently my regex is as follows:

var rx=/([\[\{<])([\s\S]+?)([\]\}>])]/; 

The problem is that such a regular expression does not make the brackets match. For example, in the following line:

 [{lastName},{firstName}] 

regex will match [{lastName}

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

+5
source share
4 answers

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 = /{([^}]+)}/; 

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

+16
source

you can use alternatives using the channel symbol ( | ), like this /\[([\s\S]+?)\]|\{([\s\S]+?)\}|<([\s\S]+?)>/ , Although it is quite long.

EDIT: shorten regex, it's not that long ...

+4
source
 var rx = /\[[^\]]+\]|\{[^}]+\}|<[^>]+>/; 
+2
source

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.

+1
source

All Articles