Intersection
It’s possible to matche the intersection of several character sets, using the & operator.
Syntax
Section titled “Syntax”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]).
Example
Section titled “Example”[Nd] & [Greek][Nd] & ![Greek]Support
Section titled “Support”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.
Behavior
Section titled “Behavior”An intersection of two expression matches a text if both expressions match. Therefore, A & B
is equivalent to (>> A) B.
Compilation
Section titled “Compilation”Intersection is compiled to an intersecting character class. For example, [w] & [n] is compiled to
[\w&&\n]. [w] & ![n] is compiled to [\w&&[^\n]].
Issues
Section titled “Issues”- 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.
History
Section titled “History”- Initial implementation in Pomsky 0.12