var str1 = "18734903350"; var str2 = "1269128116@qq.com"; var str3 = "7fgametest"; var is_email = function (text) { var pattern = /^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]*\.)+[A-Za-z]{2,14}$/; return pattern.test(text); }; var is_phonenumber = function (text) { var pattern = /^((13\d|14[57]|15[^4,\D]|17[678]|18\d)\d{8}|170[059]\d{7})$/; return pattern.test(text); }; var PhStr = "/(\d{3})\d{4}(\d{4})/"; var blindPhoneNum = function(phonenum){ return phonenum.replace(/^(\d{3})\d{4}(\d{4})$/,'$1****$2'); }; var blindMail1 = function(mail){ return mail.replace(/^(.{4})(.*)(.{1})$/,'$1****$3'); }; var blindMail2 = function(mail){ return mail.replace(/^(.{2})(.*)(.{1})$/,'$1****$3'); }; var blindMail3 = function(mail){ return mail.replace(/^(.{1})(.*)$/,'$1****$2'); }; str2 = "duhuo@163.com"; if (is_email(str2)) { var strArr = str2.split("@"); var e_str = strArr[0]; if (e_str.length > 6) { e_str = blindMail1(e_str); } else { e_str = blindMail2(e_str); } console.log("EEEEEEEEEEEEEEEE"); console.log(strArr); console.log("邮箱显示数据:", e_str + "@" + strArr[1]); } if (is_phonenumber(str1)) { var p_str = blindPhoneNum(str1); console.log("电话号码验证:", p_str); } str3 = "eddie木"; var arr = str3.length; var t_str = null; if (arr > 6) { t_str = blindMail1(str3); } else if (arr >= 3) { t_str = blindMail2(str3); } else { t_str = blindMail3(str3); } console.log("普通玩家验证:", t_str);
参考:http://www.cnblogs.com/rubylouvre/archive/2010/03/09/1681222.html