我一直致力于将用户输入转换为可读形式的电话号码的代码。当用户输入电话号码时,我没有擦洗任何东西,但我确实尝试在输出上将其擦洗回屏幕。下面是我正在使用的辅助函数。有没有人看到这个没有捕获的案例?
var formatPhone = function(raw){
var parts = raw.replace(/[^0-9X]/gi, '').split('x');
var result = parts[0];
switch(result.length){
case(13):
result = result.replace(/(\d{3})(\d{3})(\d{3})(\d{4})/, '+$1 ($2) $3-$4');
break;
case(12):
result = result.replace(/(\d{2})(\d{3})(\d{3})(\d{4})/, '+$1 ($2) $3-$4');
break;
case(11):
result = result.replace(/(\d{1})(\d{3})(\d{3})(\d{4})/, '$1 ($2) $3-$4');
break;
case(10):
result = result.replace(/(\d{3})(\d{3})(\d{4})/, '($1)v$2-$3');
break;
case(7):
result = result.replace(/(\d{3})(\d{4})/, '$1-$2');
break;
default:
result = result;
}
if(parts.length>1){
result += ' ext ' + parts[1];
}
return new Handlebars.SafeString(result);
};
代码的一般说明:
听起来对吗?我应该抓住;还是其他特殊字符?