我有一个电话号码,例如 (123) 456-7891。我需要像 1234567891 这样的号码。
我怎样才能在 Javascript 中做到这一点?
为了安全起见,我建议删除除+
(国家代码)和数字以外的所有内容:
result = subject.replace(/[^+\d]+/g, "");
您可以使用String.replace()来执行此操作。向它传递一个匹配除数字以外的所有内容的正则表达式,并将它们替换为''
.
var number = '(123) 456-7891';
number = number.replace(/[^\d]/g, '');
alert("(123) 456-7891".replace(/[\(\)\-\s]+/g, ''));