【问题标题】:JavaScript remove spaces, country code and begging zero from contact numberJavaScript 从联系电话中删除空格、国家代码和乞求零
【发布时间】:2019-04-02 05:52:41
【问题描述】:

我有联系人列表,需要从手机号码中删除国家代码 (+91)、数字和零之间的空格(前缀为零)。它应该只包含 10 位数字。

我尝试通过以下方式使用正则表达式,但它只删除数字中的空格。

var value = "+91 99 16 489165";
var mobile = '';
if (value.slice(0,1) == '+' || value.slice(0,1) == '0') {
    mobile = value.replace(/[^a-zA-Z0-9+]/g, "");
} else {
    mobile = value.replace(/[^a-zA-Z0-9]/g, "");
}

console.log(mobile);

【问题讨论】:

    标签: javascript regex string replace


    【解决方案1】:
    var value = "+91 99 16 489165";
    var number = value.replace(/\D/g, '').slice(-10);
    

    【讨论】:

      【解决方案2】:

      如果您确定“+”或“0”后有国家/地区代码,则可以使用 string.substr。

      var value="+91 99 16 489165";
      var mobile = '';
      if(value.charAt(0) == '+' || value.charAt(0)=='0'){
          mobile = value.replace(/[^a-zA-Z0-9+]/g, "").substr(3);
      }
      else {
          mobile = value.replace(/[^a-zA-Z0-9]/g, "");
      }
      

      【讨论】:

        【解决方案3】:
        var value="+91 99 16 489165";
        // Remove all spaces
        var mobile = value.replace(/ /g,'');
        
        // If string starts with +, drop first 3 characters
        if(value.slice(0,1)=='+'){
               mobile = mobile.substring(3)
            }
        
        // If string starts with 0, drop first 4 characters
        if(value.slice(0,1)=='0'){
               mobile = mobile.substring(4)
            }
        
        console.log(mobile);
        

        【讨论】:

          【解决方案4】:

          我希望这会有所帮助:

          var value = "+91 99 16 489165";
          var mobile = "";
          
          //First remove all spaces:
          value = value.replace(/\s/g, '');
          
          
          // If there is a countrycode, this IF will remove it..
          if(value.startsWith("+")){
            var temp = value.substring(3, value.length);
            mobile = "0"+temp;
            
            //Mobile number:
            console.log(mobile);
          }
          
          
          // If there is no countrycode, only remove spaces
          else{
            mobile = value;
            
            //Mobile number:
            console.log(mobile);
          }

          【讨论】:

            【解决方案5】:

            这是我用来仅删除电话号码的国家/地区代码部分的正则表达式:

            var mobile = value.replace(/^\+[0-9]{1,3}(\s|\-)/, "");
            

            美国:

            +1 345 345 7678

            +1-453-677-7655

            印度:

            +91 99 16 489165

            如果您的数据中以“+”号开头,则它适用于本网站上列出的所有国家/地区代码: https://countrycode.org/

            【讨论】:

              【解决方案6】:

              删除空格并获取最后 10 位数字。

              str.replaceAll(" ","").slice(-10)
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-12-07
                • 1970-01-01
                • 1970-01-01
                • 2017-04-09
                • 2017-08-27
                • 1970-01-01
                相关资源
                最近更新 更多