是否有 JavaScript 正则表达式相当于 Java 正则表达式中的交集 (&&) 运算符?

发布于 2024-11-19 05:11:32 字数 218 浏览 2 评论 0 原文

在Java正则表达式中,您可以在字符类中使用交集运算符&&来简洁地定义它们,例如

[a-z&&[def]]    // d, e, or f
[a-z&&[^bc]]    // a through z, except for b and c

JavaScript中是否有等效的?

In Java regexes, you can use the intersection operator && in character classes to define them succinctly, e.g.

[a-z&&[def]]    // d, e, or f
[a-z&&[^bc]]    // a through z, except for b and c

Is there an equivalent in JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经 2024-11-26 05:11:32

JavaScript 中有等效的吗?

简单的回答:不,没有。它是特定的Java语法。

请参阅:正则表达式食谱,作者:Jan Goyvaerts 和 Steven Levithan。这是相关部分一览

可能不用说,但以下 JavaScript 代码:

if(s.match(/^[a-z]$/) && s.match(/[^bc]/)) { ... }

会执行与 Java 代码相同的操作:

if(s.matches("[a-z&&[^bc]]")) { ... }

Is there an equivalent in JavaScript?

Simple answer: no, there's not. It is specific Java syntax.

See: Regular Expressions Cookbook by Jan Goyvaerts and Steven Levithan. Here's a sneak-peek to the relevant section.

Probably needless to say, but the following JavaScript code:

if(s.match(/^[a-z]$/) && s.match(/[^bc]/)) { ... }

would do the same as the Java code:

if(s.matches("[a-z&&[^bc]]")) { ... }
疯到世界奔溃 2024-11-26 05:11:32

正如其他人所说,没有等效的,但您可以使用前瞻来实现 && 的效果。转换为:

[classA&&classB]

变为:

(?=classA)classB

例如,Java 中的 this:

[a-z&&[^bc]]

this: 具有相同的行为

(?=[a-z])[^bc]

与 JavaScript 中完全支持的 。我不知道这两种形式的相对性能(在支持这两种形式的 Java 和 Ruby 等引擎中)。

由于 && 运算符是可交换的,因此您始终可以使用任一侧作为(正或负)前瞻部分。

类与否定类的交集也可以通过否定前瞻来实现。所以上面的例子也可以改写为:

(?![bc])[a-z]

As others have said, there isn't an equivalent, but you can achieve the effect of && using a look-ahead. The transformation is:

[classA&&classB]

becomes:

(?=classA)classB

For instance, this in Java:

[a-z&&[^bc]]

has the same behavior as this:

(?=[a-z])[^bc]

which is fully supported in JavaScript. I don't know the relative performance of the two forms (in engines like Java and Ruby that support both).

Since the && operator is commutative, you can always use either side for the (positive or negative) look-ahead part.

Intersection of a class with a negated class can also be implemented with negative look-ahead. So the example above could also have been transformed to:

(?![bc])[a-z]
沉睡月亮 2024-11-26 05:11:32

2023回答:是的,有。

使用 v标志

[[a-z]&&[def]]
[[a-z]&&[^bc]]

尝试 regex101.com

您还可以进行减法:[[az]--[bc]]。更多信息请参阅MDN< /a>.

2023 answer: Yes, there is.

With the v flag:

[[a-z]&&[def]]
[[a-z]&&[^bc]]

Try them on regex101.com.

You can also do subtraction: [[a-z]--[bc]]. More information can be found on MDN.

韵柒 2024-11-26 05:11:32

通过直接写出字符类,您可以获得与 JavaScript 中的 Java 正则表达式相同的结果,例如,

Java           JavaScript   English
------------   ----------   -------
[a-z&&[def]]   [def]        d, e, or f
[a-z&&[^bc]]   [ad-z]       a through z, except for b and c

在某些情况下,它只是更冗长/晦涩一些,例如

Java               JavaScript
----------------   -----------
[A-Z&&[^QVX]]      [A-PR-UWYZ]
[A-Z&&[^CIKMOV]]   [ABD-HJLNP-UW-Z]

You can get the same result as the Java regexes in JavaScript by writing out the character classes longhand, e.g.

Java           JavaScript   English
------------   ----------   -------
[a-z&&[def]]   [def]        d, e, or f
[a-z&&[^bc]]   [ad-z]       a through z, except for b and c

It’s just a bit more verbose/obscure in some circumstances, e.g.

Java               JavaScript
----------------   -----------
[A-Z&&[^QVX]]      [A-PR-UWYZ]
[A-Z&&[^CIKMOV]]   [ABD-HJLNP-UW-Z]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文