Javascript 正则表达式匹配括号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11907275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to match brackets
提问by Christophe
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 <tag> or [element].
对于模板引擎,我使用正则表达式来标识字符串中括号下的内容。例如,正则表达式需要匹配 {key} 或 <tag> 或 [element]。
Currently my regular expression looks like this:
目前我的正则表达式如下所示:
var rx=/([\[\{<])([\s\S]+?)([\]\}>])]/;
The issue is that such a regular expression doesn't force brackets to match. For example in the following string:
问题是这样的正则表达式不会强制括号匹配。例如在以下字符串中:
[{lastName},{firstName}]
the regular expression will match [{lastName}
正则表达式将匹配 [{lastName}
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 >
有没有办法定义匹配的括号?例如,如果左括号是 [ 则右括号必须是 ],而不是 } 或 >
回答by murgatroid99
The best way to do this, especially if different brackets can have different meanings, is to split into 3 regular expressions:
做到这一点的最好方法,特别是如果不同的括号可以具有不同的含义,是将其拆分为 3 个正则表达式:
var rx1 = /\[([^\]]+)]/;
var rx2 = /\(([^)]+)\)/;
var rx3 = /{([^}]+)}/;
These will match any text surrounded by []
, ()
, and {}
respectively, with the text inside in the first matched group.
这些将分别匹配由[]
、()
和包围的任何文本,以及{}
第一个匹配组中的文本。
回答by StrubT
you could use alternatives using pipe character (|
) like this one /\[([\s\S]+?)\]|\{([\s\S]+?)\}|<([\s\S]+?)>/
, although it gets pretty long.
您可以使用|
像这样的管道字符 ( )替代方案/\[([\s\S]+?)\]|\{([\s\S]+?)\}|<([\s\S]+?)>/
,尽管它变得很长。
EDIT:shortend the regex, is not that long any more...
编辑:缩短正则表达式,不再那么长......
回答by MikeM
var rx = /\[[^\]]+\]|\{[^}]+\}|<[^>]+>/;
回答by ghoti
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.
ERE 没有提供一种方法让您按照您描述的方式将右括号与左括号匹配。(使用 PREG 魔法可能是可能的,但我必须把它留给其他人。)您需要有多个正则表达式,或单个正则表达式中的多个原子。
If you use a single regex, I gather you'll need to determine the typeof 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.
在下面的正则表达式中,每种字符串样式都表示为 RE 中的一个“分支”。分支由 or 横杠 ( |
)分隔。为清楚起见,我假设所有字符串都是[:alnum:]
. 您尚未指定内容,因此您需要根据您的特定要求进行调整。
/(\[)([[:alnum:]]+)\]|(\()([[:alnum:]]+)\)|(\{)([[:alnum:]]+)\}/
↑ ↑ ↑ ↑
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:
请注意,在每个分支中,第一个字符用圆括号括起来,使其成为“原子”。你需要你的代码像反向引用一样引用这个原子。第二个原子是内部字符串。现在……我的 JavaScript 不如我的烘焙技能那么强大,但这可能是一个开始:
String.prototype.bracketstyle = function() {
var re = /(\[)([:alnum:]+)\]|(\()([:alnum:]+)\)|(\{)([:alnum:]+)\}/;
return this.replace(re,"");
}
String.prototype.innerstring = function() {
var re = /(\[)([:alnum:]+)\]|(\()([:alnum:]+)\)|(\{)([:alnum:]+)\}/;
return this.replace(re,"");
}
I suspect you could combine these into a single function, or use them differently without making them a function, but you get the idea.
我怀疑您可以将这些组合成一个函数,或者在不将它们变成函数的情况下以不同的方式使用它们,但是您明白了。