I’m afraid that refinement on a match of a user-input string will result in an error, since matching with a regex that has the g flag set and doesn’t match the string will return null, which is unrefinable.
So I’m left with the two following options, and as far as I can tell, they’re identical. But I just want to make sure I’m understanding it properly.
var tldMatcher = /[^.]+\.?$/g
, tld = str.match(tldMatcher) ? str.match(tldMatcher)[0] : null;
…and…
var tldMatcher = /[^.]+\.?$/g
, tld = str.match(tldMatcher) && str.match(tldMatcher)[0];
Any thoughts?
Personally I always use
|| []when I use.matchwithg. That is:This ensures
matcheswill always be an array of strings (but possibly empty). In your case it could be done like this: