mm-utils
Version:
mm-utils,JavaScript工具包,日常开发工作常用的公共函数库
271 lines (254 loc) • 6.44 kB
JavaScript
// 正则处理库
/**
* 判断是否包含表情字符
* @method hasEmoji
* @param {string} str - 源数据
* @returns {boolean}
*/
function hasEmoji(str) {
if (typeof str == "string" && str !== "") {
let emojiRule = /[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig;
return emojiRule.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否包含特殊字符
* @method hasSpecialChar
* @param {string} str - 源数据
* @returns {boolean}
*/
function hasSpecialChar(str) {
if (typeof str == "string" && str !== "") {
let specialRule = /[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~!@#¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、]/im;
return specialRule.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否包含中文字符
* @method hasChineseChar
* @param {string} str - 源数据
* @returns {boolean}
*/
function hasChineseChar(str) {
if (typeof str == "string" && str !== "") {
let reg = /[\u4e00-\u9fa5]+/g;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否包含数字
* @method hasNumber
* @param {string} str - 源数据
* @returns {boolean}
*/
function hasNumber(str) {
if (typeof str == "string" && str !== "") {
let reg = /\d/g;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否包含字母
* @method hasLetter
* @param {string} str - 源数据
* @returns {boolean}
*/
function hasLetter(str) {
if (typeof str == "string" && str !== "") {
// let specialRule = /^[#a-zA-Z][a-zA-Z || _ || \d]{1,}$/;//只针对纯字母才返回true
let specialRule = /[a-zA-Z]/g;
return specialRule.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为小数
* @method isDecimal
* @param {string} str - 源数据
* @param {number} num - 小数位数
* @returns {boolean}
*/
function isDecimal(str, num = -1) {
//判断是否为数字类型
let regex = /^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
if (regex.test(str)) {
if (num !== -1) {
str = str.toString();
let index = str.lastIndexOf(".");
str = str.substring(index + 1, str.length);
return str.length == num;
} else {
return true;
}
} else {
return "格式错误";
}
}
/**
* 判断是否为url
* @method isUrl
* @param {string} str - 源数据
* @returns {boolean}
*/
function isUrl(str) {
if (typeof str == "string" && str !== "") {
let reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为isEmail
* @method isEmail
* @param {string} str - 源数据
* @returns {boolean}
*/
function isEmail(str) {
if (typeof str == "string" && str !== "") {
let reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为身份证号
* @method isIdcard
* @param {string} str - 源数据
* @param {number} mode - 0大陆;1香港;2澳门;3台湾
* @returns {boolean}
*/
function isIdcard(str, mode = 0) {
let idCardChinaReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
let idCardHongkongReg = /^((\s?[A-Za-z])|([A-Za-z]{2}))\d{6}(\([0−9aA]\)|[0-9aA])$/; //香港id:由一个英文字母+6位数字+一位验证码(括号内数字)组成
let idCardMacaoReg = /^[1|5|7][0-9]{6}\([0-9Aa]\)/;
let idCardTaiwanReg = /^[a-zA-Z][0-9]{9}$/;
if (mode == 0 || mode == "") {
return idCardChinaReg.test(str);
} else if (mode == 1) {
return idCardHongkongReg.test(str);
} else if (mode == 2) {
return idCardMacaoReg.test(str);
} else if (mode == 3) {
return idCardTaiwanReg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为中文字符
* @method isChineseChar
* @param {string} str - 源数据
* @returns {boolean}
*/
function isChineseChar(str) {
if (typeof str == "string" && str !== "") {
let reg = /^[\u4e00-\u9fa5]+$/i;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为数字
* @method isNumber
* @param {string} str - 源数据
* @returns {boolean}
*/
function isNumber(str) {
if (str === "") {
return "格式错误";
} else {
let reg = /^[+-]?[1-9]?[0-9]*\.[0-9]*$/;
return reg.test(str);
}
}
/**
* 判断是否为IP
* @method isIP
* @param {string} str - 源数据
* @returns {boolean}
*/
function isIP(str) {
if (typeof str == "string" && str !== "") {
let reg = /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/i;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为字母
* @method isLetter
* @param {string} str - 源数据
* @returns {boolean}
*/
function isLetter(str) {
if (typeof str == "string" && str !== "") {
let reg = /[a-zA-Z]+/;
return reg.test(str);
} else {
return "格式错误";
}
}
/**
* 判断是否为手机号码
* @method isPhone
* @param {string} str - 源数据
* @param {number} mode - 0手机号码;1电话号码
* @returns {boolean}
*/
function isPhone(str, mode = 0) {
if (str === "") {
return "格式错误";
} else {
if(mode == 0 ){
let reg = /^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
return reg.test(str);
}else{
let reg =/^([0-9]{3,4}-)?[0-9]{7,8}$/;
return reg.test(str);
}
}
}
/**
* 判断是否为邮政编码
* @method isPostcode
* @param {string} str - 源数据
* @returns {boolean}
*/
function isPostcode(str) {
if (str === "") {
return "格式错误";
} else {
let reg = /^[0-9]{6}$/;
return reg.test(str);
}
}
export default {
hasEmoji,
hasSpecialChar,
hasChineseChar,
hasNumber,
hasLetter,
isDecimal,
isUrl,
isEmail,
isIdcard,
isChineseChar,
isNumber,
isIP,
isLetter,
isPhone,
isPostcode,
};