Here is one way to do this:
function formatPhoneNumber(input, format) { // Strip non-numeric characters var digits = input.replace(/\D/g, ''); // Replace each "X" with the next digit var count = 0; return format.replace(/X/g, function() { return digits.charAt(count++); }); }
Matthew Crumley
source share