Javascript String Replace Digit to Word

Description

Javascript String Replace Digit to Word


let SearchText = "my phone number is 219.423.4432. Office: 219 482 3423, home: 219-843-1244.";
let myRegexp = /\b(\d{3})([- \.])(\d{3})\2(\d{4})\b/g;

let resultText = SearchText.replace(myRegexp, function(match) {
  let NumberWords = {'0':'Zero', '1':'One', '2':'Two', '3':'Three', '4':'Four', '5':'Five', '6':'Six', '7':'Seven', '8':'Eight', '9':'Nine'};
  for (num in NumberWords)
    match = match.replace(new RegExp(num, "g"), NumberWords[num]);
  return match;//from   ww  w .  j a va 2s.c  o  m
});

console.log( resultText );



PreviousNext

Related