正则表达式:不要在组中包含子字符串
我想知道是否可以在组中排除比赛的一部分。
我相信/(foo((?:bar)|(bad)))/
导致匹配“ foobar”或“ foobad”和包含“ foobar”或“ foobad”的第1组。我希望它匹配“ Foobar”或“ Foobad”,但第1组仅包含“ Foo”或“ Foobad”。我知道 (?:...)
正则表达式语法用于创建一个稍后无法引用的组,但上述用法意味着有 3 个组: \1 整个匹配; \ 2“ bar”或“坏”; \没有一个未使用的团体“ bar”; \ 3为“坏”。
你看到我要去哪里?我想选择地从另一组中排除一个子组。
或者,我想以 OR 方式定义组 1 两次 /(foobar)|(foobad)/
尽管我知道输入不会执行我想要的操作。在这种情况下,尝试访问匹配[1]
如果给出的“ foobad”作为输入,则会导致“未定义”错误。我知道这是因为它实际上将其分为第1组和 /或组2
。
I'm wondering if it is possible to exclude part of a match within a group.
I believe that /(foo((?:bar)|(bad)))/
results in matching "foobar" or "foobad" and group 1 containing "foobar" or "foobad". I would like it to match "foobar" or "foobad" but group 1 contain only "foo" or "foobad". I know that the (?:...)
regex syntax is used to make a group that you cannot reference later on, but the above usage means that there are 3 groups: \1 the entire match; \2 either 'bar' or 'bad'; \NONE being an unused group 'bar'; and \3 for 'bad'.
You see where I'm going here? I want to optionally exclude a sub group from within another group.
Alternatively, I would like to define group 1 twice in an OR fashion /(foobar)|(foobad)/
though I know that input wouldn't do what I wanted. In that case, trying to access match[1]
results in an "undefined" error if given "foobad" as input. I know this is because it is actually splitting it into group 1 and or group 2.
I'm writing this in the javascript regex dialect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正则表达式:
的作用是:组 1 要么包含
foo
(仅当bar
紧随其后),要么包含foobad
。The regex:
does that: group 1 either contains
foo
(only ifbar
comes directly after it) or it containsfoobad
.(?:bar)
- 匹配 bar 但不记得匹配。这些称为非捕获括号。无法从结果数组的元素[1], ..., [n]
或预定义的 RegExp 对象的属性$1, ..., $9
中调用匹配的子字符串>。更多信息:Mozilla 开发人员中心。
(?:bar)
- Matches bar but does not remember the match. These are called non-capturing parentheses. The matched substring can not be recalled from the resulting array's elements[1], ..., [n]
or from the predefined RegExp object's properties$1, ..., $9
.Further info: Mozilla Developer Center.
这很接近您想要的,
它是一个匹配
foo
(但前提是从它开始匹配foobar
)或foobad
的单个组>。(?=...)
形式是零宽度先行断言。这并不完全是你想要的,因为
foobar
的情况下的匹配只是foo
但 IMO 能够让它匹配 foobar 但只将 foo 分组在同一组中,其中foobad 会被匹配,而不需要lookbehind,并且据我所知,它在javascript中不可用。This is close to what you want
it's a single group that matches either
foo
(but only if starting with that it would have matchedfoobar
) orfoobad
. The form(?=...)
is a zero-width look-ahead assertion.It's not exactly what you want because the match in case of
foobar
is justfoo
but IMO to be able to have it to match foobar but grouping only foo in the same group where foobad would be matched instead requires lookbehind and AFAIK it's not available in javascript.