記錄一下前端常用正則表達(dá)式,實(shí)際工作中要根據(jù)自己的項(xiàng)目提取合適的去使用
let regExp = {
// 數(shù)字正則
isNumber: function(str) {
let reg = /^-?\d*\.?\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 正數(shù)正則
isPosNumber: function(str) {
let reg = /^\d*\.?\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 負(fù)數(shù)正則
isNegNumber: function(str) {
let reg = /^-\d*\.?\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 整數(shù)正則
isInteger: function(str) {
let reg = /^-?\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 正整數(shù)正則
isPosInteger: function(str) {
let reg = /^\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 負(fù)整數(shù)正則
isNegInteger: function(str) {
let reg = /^-\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 用戶名正則
isUserName: function(str) {
// 4到16位(字母,數(shù)字,下劃線,減號(hào))
let reg = /^[a-zA-Z0-9_-]{4,16}$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 包含中文正則
isChinese: function(str) {
let reg = /[\u4E00-\u9FA5]/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 密碼正則
isPassword: function(str) {
// 以字母開頭,長(zhǎng)度在6~18之間,只能包含字母、數(shù)字和下劃線
let reg = /^[a-zA-Z]\w{5,17}$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 郵箱正則
isEmail: function(str) {
let reg = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 手機(jī)號(hào)正則
isPhone: function(str) {
// 移動(dòng)號(hào)段:134 135 136 137 138 139 147 148 150 151 152 157 158 159 172 178 182 183 184 187 188 198
// 聯(lián)通號(hào)段:130 131 132 145 146 155 156 166 171 175 176 185 186
// 電信號(hào)段:133 149 153 173 174 177 180 181 189 199
// 虛擬運(yùn)營(yíng)商:170
let reg = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 固定電話正則
isTelephone: function(str) {
let reg = /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 第二代身份證號(hào)正則
isIDCard: function(str) {
let reg = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// QQ號(hào)正則
isQQNumber: function(str) {
let reg = /^[1-9][0-9]{4,10}$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 微信號(hào)正則
isWeChatNumber: function(str) {
// 6至20位,以字母開頭,字母,數(shù)字,減號(hào),下劃線
let reg = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 特殊字符正則
isSpeNumber: function(str) {
let reg = /["'<>%;)(&+]+-!!@#$~/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// IP地址正則
isIP: function(str) {
let reg = /^\d+\.\d+\.\d+\.\d+$/;
if (reg.test(str)) {
return true
} else {
return false
}
},
// 郵政編碼正則
isPostCode: function(str) {
let reg = /^[1-9]{1}(\d+){5}$/;
if (reg.test(str)) {
return true
} else {
return false
}
}
};
附上我比較喜歡用的一個(gè)正則圖形化網(wǎng)站,有興趣的可以試試:正則圖形化
參考資料:
如何判斷用戶瀏覽器以及一些前端常用的正則表單驗(yàn)證
前端開發(fā)中的 正則表達(dá)式 及常用正則表達(dá)式大全