Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding first alphabetical letter in a string - Javascript

I've done some research and can't seem to find a way to do this. I even tried using a for loop to loop through the string and tried using the functions isLetter() and charAt(). I have a string which is a street address for example:

var streetAddr = "45 Church St";

I need a way to loop through the string and find the first alphabetical letter in that string. I need to use that character somewhere else after this. For the above example I would need the function to return a value of C. What would be a nice way to do this?

like image 631
fadim Avatar asked Nov 29 '22 07:11

fadim


1 Answers

Maybe one of the shortest solutions:

'45 Church St'.match(/[a-zA-Z]/).pop();

Since match will return null if there are no alphanumerical characters in a string, you may transform it to the following fool proof solution:

('45 Church St'.match(/[a-zA-Z]/) || []).pop();
like image 182
VisioN Avatar answered Nov 30 '22 19:11

VisioN