简体   繁体   中英

Regex for Comma delimited list of domain

I am looking for the Regex to validate the list of domains which have comma as separator. For example:

yahoo.com, google.com, facebook.net

The code for single domain is:

^\\S*\\b((?=[a-z0-9-]{1,63}.)(xn--)?[a-z0-9]+(-[a-z0-9]+) .)+[az]{2,63}\\S $\\b/

Appreciate your help

I think it is better to split them and use Array.prototype.every()

The every() method tests whether all elements in the array pass the test implemented by the provided function.

in the following way:

 var validDomains = 'yahoo.com, google.com, facebook.net'; var invalidDomains = 'yahoo.c, google.com, facebook.net'; function checkDomains(domains){ return domains.split(',').every(function(d){ var reg = /[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\\.[a-zA-Z]{2,}/; return reg.test(d.trim()); }); } console.log(checkDomains(validDomains)); console.log(checkDomains(invalidDomains));

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