Last Updated: February 25, 2016
·
260
· scottbarstow

Finding a string in a phone number

How to find a string in a phone number and replace it. For example, given the string 'code' and the phone number '8556212633 this function will return back '855621code'

function alphabetizeNumber(number,str) { var x = str.toString() .replace(/a|b|c/gi,'2') .replace(/d|e|f/gi,'3') .replace(/g|h|i/gi,'4') .replace(/j|k|l/gi,'5') .replace(/m|n|o/gi,'6') .replace(/p|q|r|s/gi,'7') .replace(/t|u|v/gi,'8') .replace(/w|x|y|z/gi,'9'); return(number.toString().replace(x,str)); }

@andersbrownwort did most/all of the work on this. I just wanted to make sure it was recorded somewhere so I don't forget it later.