简体   繁体   中英

Allow regex pattern to be repeated if it is comma-separated?

I have a JavaScript regex for email addresses:

/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

Fiddle here: https://regex101.com/r/8Z9ORb/1

Used in the .Validate plugin, this will allow test@test.com but not test@test and other bad email addresses.

But how can I allow this pattern to be repeated if comma separated? Ie, how can I allow:

  • test@test.com
  • test@test.com,example@exmaple.com
  • test@test.com, example@exmaple.com, Nth@moreemails.com (note the spaces)

While not allowing bad emails in the CSV or a bad CSV, like:

  • test@test.com,testtest.com
  • test@test.com and test2@test2.com

This seems to work? But I'm no regexpert.

/^((([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))+(,|, |$))*$/

https://regex101.com/r/8Z9ORb/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM