Skip to content
You are viewing the next version of this website. See current version

Intersection

It’s possible to matche the intersection of several character sets, using the & operator.

let Intersection = '&'? Sequence ('&' Sequence)*;
let Sequence = FixExpression+;

See FixExpression.

Only character sets – and variables that resolve into character sets – can be intersected. The character sets can be negated.

Intersection has a higher operator precedence than alternation: "test" | [Greek] & [Nd] is parsed as "test" | ([Greek] & [Nd]).

[Nd] & [Greek]
[Nd] & ![Greek]

Intersection is supported in the JavaScript, Java, Ruby, and Rust flavors. In JavaScript, the v flag is required for intersection to work.

Support for intersection is gated by the intersection feature. Specify features with the --allowed-features option.

An intersection of two expression matches a text if both expressions match. Therefore, A & B is equivalent to (>> A) B.

Intersection is compiled to an intersecting character class. For example, [w] & [n] is compiled to [\w&&\n]. [w] & ![n] is compiled to [\w&&[^\n]].

  • Intersection could be polyfilled in more regex flavors using lookahead, but this is not done at the moment.
  • Intersection of an alternation is not allowed even if it would be optimized to a character class.
  • Assertions (anchors, lookarounds) are not allowed in an intersection, even though they could be supported.
  • Initial implementation in Pomsky 0.12