如何在正则表达式中结合2个条件和更多条件 [英] How to combine 2 conditions and more in regex
问题描述
我想创建一个匹配'。','#'和':'的正则表达式,并且还匹配这些括号中的所有内容'['& ']',例如[foo]& [bar]
I want to create a regex that match '.', '#' and ':' and also match everything inside these brackets '[' & ']' such as [foo] & [bar]
我已经有这个匹配 string.match(/ [。#:] / g)
for '。','#'和':'。
I already have this match string.match(/[.#:]/g)
for '.','#' and ':'.
我知道括号正则表达式应该如下所示 \ [。\\\]
I know that the brackets regex should look like this \[.\]
但如何将它们组合成一个条件?
but how do I combine them both to one condition?
谢谢,
Alon
thanks, Alon
推荐答案
将它们合并使用
/[.#:]|(?:\[.+?\])/g
?:是可选的,用于不捕获组(括号中的任何内容)
?: is optional and is used to not capture the group (anything in parenthesis)
更新:
。+?
(一个或多个)或。*?
(零或更多) - 使用它进行延迟匹配,否则 [ sdfsdf] [sdfsddf ] 将匹配
.+?
(one or more) or .*?
(for zero or more)- use this for lazy matching, otherwise [ sdfsdf][sdfsddf ] will be matched
这篇关于如何在正则表达式中结合2个条件和更多条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!