According to the documentation,
exec
returns either array or null, but not undefined. So, if
(results == null) return "";
could not cause a problem. Lets look at "else" part. If
results
has less then two members, you could have undefined return from your function. If you could use JavaScript debugger, you would easily find it out.
So, as to the first point, the condition under "if", I would prefer to be on the safe side, so I would write
if (results) {
if (results.length > 1)
return results[1];
else
return "";
} else
return "";
Instead of returning "" in two cases, you could return something different, to distinguish the case when the array is not long enough to have an element at [1], and the case where there is no a match at all. That's why I suggested to return null in this case, "" in another one. Or something like that. Also, you could think of checking up
name
for null and then empty string before using Regex, to pass the same values to return.
—SA