I've got this javascript code...
function getPhone( str )
{
var regExp = /(+1)(()\d{3}())* \d{3}-\d{4}/gi;
if (regExp.test( str ))
{
var ar = regExp.exec( str );
return ar[0];
}
else
return "";
}
.... and it's supposed to return a phone number if it exists in the string. Before I put the if statement in I just had it exec and then return ar[0] because I was putting a phone number in the string to test it. It worked fine. But now with the if statement (which I need in case there is no match), I get an error that says "0 is null or not an object". I can only assume it's talking about trying to reference the 0 position in the array. And it only errors when there is a match. Is there a way I can just execute it and see if ar is an array before trying to return the ar[0] element? Thanks.