简体   繁体   English

Javascript正则表达式:括在括​​号中时忽略右括号

[英]Javascript regex: Ignore closing bracket when enclosed in parentheses

I have a regex with a couple of (optional) capture groups.我有一个带有几个(可选)捕获组的正则表达式。 I'm trying to add a new feature that allows a user to add content to one of the capture groups that matches the closing bracket of the regex.我正在尝试添加一项新功能,该功能允许用户将内容添加到与正则表达式的右括号匹配的捕获组之一。 I'm struggling to ignore this match我正在努力忽略这场比赛

The current regex is /\\[(.+?)=(.+?)(?:\\|(.+?))?(?:\\:(.+?))?\\]/g当前的正则表达式是/\\[(.+?)=(.+?)(?:\\|(.+?))?(?:\\:(.+?))?\\]/g

This allows a user to target data according to:这允许用户根据以下条件定位数据:

[key=value|filter:filtervalue]

where filter and filtervalue are optional.其中 filter 和 filtervalue 是可选的。

The problem is that for the value it should now be possible to target indexes in an array.问题是对于该值,现在应该可以在数组中定位索引。 For example:例如:

[data=example(products[0].id)]

However, the regex matches only up to .id so the second capture group is example(products[0 . I would like it to be example(products[0].id) . I think I should be fine if I can ignore the closing bracket when it is wrapped by parentheses, but I've been unable to figure out how.但是,正则表达式只匹配到.id所以第二个捕获组是example(products[0 。我希望它是example(products[0].id) 。如果我可以忽略关闭,我想我应该没问题当它被括号包裹时括号,但我一直无法弄清楚如何。

Examples that should be matched:应匹配的示例:

[data=example(products[0].id)]
[data=example(products[index].id)]
[data=regular]
[data=test|number:2]

I created a regex101 .我创建了一个regex101 Any help is appreciated.任何帮助表示赞赏。

You may use您可以使用

/\[([^=\]]+)=((?:\[[^\][]*]|[^\]])+?)(?:\|([^:\]]+):((?:\[[^\][]*]|[^\]])+?))?]/g

See the regex demo查看正则表达式演示

Note that lazy dot matching patterns are replaced with more restrictive negated character classes to make sure there is no overflow from one part of the bracketed string to another.请注意,惰性点匹配模式被更严格的否定字符类替换,以确保括号字符串的一部分不会溢出到另一部分。 To allow matching 1st-level nested [...] substrings, a \\[[^\\][]*]|[^\\]] pattern is used to match [...] or non- ] substrings is used.为了允许匹配第一级嵌套的[...]子字符串, \\[[^\\][]*]|[^\\]]模式用于匹配[...]或非]子字符串。

Details :详情

  • \\[ - a literal [ \\[ - 文字[
  • ([^=\\]]+) - Group 1 capturing 1+ chars other than = and ] ([^=\\]]+) - 第 1 组捕获 1+ 个字符,而不是=]
  • = - a = symbols = - a =符号
  • ((?:\\[[^\\][]*]|[^\\]])+?) - Group 2 capturing 1+ occurrences (as few as possible) of: ((?:\\[[^\\][]*]|[^\\]])+?) - 第 2 组捕获 1+ 次(尽可能少):
    • \\[[^\\][]*] - [ , then 0+ chars other than ] and [ , and then ] \\[[^\\][]*] - [ ,然后是][以外的 0+ 个字符,然后是]
    • | - or - 或者
    • [^\\]] - any char other than ] [^\\]] - 除]以外的任何字符
  • (?:\\|([^:\\]]+):((?:\\[[^\\][]*]|[^\\]])+?))? - an optional group matching: - 可选的组匹配:
    • \\| - a | - 一个| char字符
    • ([^:\\]]+) - Group 3 capturing 1+ chars other than : and ] ([^:\\]]+) - 第 3 组捕获:]以外的 1+ 个字符
    • : - a colon : - 一个冒号
    • ((?:\\[[^\\][]*]|[^\\]])+?) - Group 4 capturing the same text as Group 2. ((?:\\[[^\\][]*]|[^\\]])+?) - 第 4 组捕获与第 2 组相同的文本。
  • ] - a literal ] char. ] - 文字]字符。

I would probably break it down to two separate regular expressions and "or" them.我可能会将它分解为两个单独的正则表达式并“或”它们。 Below I have used your expression to match the first kind of string:下面我用你的表达式来匹配第一种字符串:

\[(?:(.+?)=(.+)(?:\|(.+?)?\:(.+?)))\]
[key=value|filter:filtervalue]

And another for the second one:另一个是第二个:

\[(.+?)=(.+)\]
[data=example(products[0].id)]

Then concatenating them to:然后将它们连接到:

\[(?:(.+?)=(.+)(?:\|(.+?)?\:(.+?))|(.+?)=(.+))\]

Where it first tries to match the tricky part and if that fails, resorts to the more general one.它首先尝试匹配棘手的部分,如果失败,则求助于更通用的部分。

https://regex101.com/r/d6LwEt/1 https://regex101.com/r/d6LwEt/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM