It seems to me you'd better split the string up in pieces with a regex like \s*,\s*
first, and then validate each domain seperately. Trying to push it all into a single regex will make your life hard if you try to change the logic later.
The domain validating regex that you have should work. I don't know if you are intentionally disallowing some valid domains. A more generic domain matching regex would be:
^[\w-]+(\.[\w-]+)+$
But even this one disallows localhost
, which may or may not be allowed in your case. It is too loose for the specs in other respects. Optionally replace each [\w-]+
with [a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?
for more strictness and more obfuscation -- although it still does not invalidate numeric-only records, which are invalid according to the spec. How exact do you need it to be?
Disregarding the stuff about splitting the string first, based on my example regex above, you could use the following monster to match a list of domains:
^\s*([\w-]+(\.[\w-]+)+\s*,\s*)*[\w-]+(\.[\w-]+)+\s*$