Regex to detect one of several strings

前端 未结 7 1688
野趣味
野趣味 2020-12-08 13:20

I\'ve got a list of email addresses belonging to several domains. I\'d like a regex that will match addresses belonging to three specific domains (for this example: foo, bar

相关标签:
7条回答
  • 2020-12-08 13:43

    Use:

    /@(foo|bar|baz)\.?$/i
    

    Note the differences from other answers:

    • \.? - matching 0 or 1 dots, in case the domains in the e-mail address are "fully qualified"
    • $ - to indicate that the string must end with this sequence,
    • /i - to make the test case insensitive.

    Note, this assumes that each e-mail address is on a line on its own.

    If the string being matched could be anywhere in the string, then drop the $, and replace it with \s+ (which matches one or more white space characters)

    0 讨论(0)
提交回复
热议问题