Regular Expressions: Is there an AND operator?

后端 未结 12 2026
刺人心
刺人心 2020-11-21 06:34

Obviously, you can use the | (pipe?) to represent OR, but is there a way to represent AND as well?

Specifically, I\'d like to

12条回答
  •  心在旅途
    2020-11-21 07:12

    The AND operator is implicit in the RegExp syntax.
    The OR operator has instead to be specified with a pipe.
    The following RegExp:

    var re = /ab/;
    

    means the letter a AND the letter b.
    It also works with groups:

    var re = /(co)(de)/;
    

    it means the group co AND the group de.
    Replacing the (implicit) AND with an OR would require the following lines:

    var re = /a|b/;
    var re = /(co)|(de)/;
    

提交回复
热议问题