正则效验从身份证中提取性别和生日

// 从身份证中提取生日
    getBirthdayFromIdCard(idCard) {  
      let birthday = "";  
      if(idCard != null && idCard != ""){  
        if(idCard.length == 15){  
            birthday = "19"+idCard.substr(6,6);  
        } else if(idCard.length == 18){  
            birthday = idCard.substr(6,8);  
        }  
        birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-");  
      }  
      return birthday;  
    },
    // 从身份证中提取性别
    getBirthdayFromSex(sex) { 
      let gender = ""; 
      if(sex != null && sex != ""){ 
      if(sex.length == 15){ 
        gender = "19"+sex.substr(6,6); 
      } else if(sex.length == 18){ 
        gender = parseInt(sex.slice(14,17));
        if(gender % 2 === 1) { // 男
          gender = 1
          } else { // 女
          gender = 2
          }
        } 
      } 
      return gender; 
    }
// 调用
let birthday =this.getBirthdayFromIdCard('身份证号')
let sex=this.getBirthdayFromSex('身份证号')

你可能感兴趣的:(正则效验从身份证中提取性别和生日)