easy-regexp
v1.0.1
Published
前端编写代码以及表单验证常用正则表达式验证和提取
Downloads
8
Maintainers
Readme
使用方法
安装
npm install easy-regexp
导入
const easyRegexp = require('easy-regexp');
功能函数
用户名校验
输入:
用户名name
用户名位数下限start(默认为6)
用户名位数下线end(默认为15)
是否可以使用特殊字符“-”或者“_”specialPermit(默认为true)
是否开启首字母不为特殊字符firstChar(默认为true)
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.nameReg('sd_s123',true,true)); //true
console.log(easyRegexp.nameReg('sd_s123',10,15,true,true)); //false
密码校验
输入:
密码password
用户名位数下限start(默认为6)
用户名位数下线end(默认为30)
是否需要包括至少1个大写字母bigLetter(默认为true)
是否需要包括至少1个小写字母smallLetter(默认为true)
是否需要包括至少1个数字number(默认为true)
是否需要包括至少1个特殊字符spesialChar(默认为true)
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.passwordReg('Mwxds89!')); //true
console.log(easyRegexp.passwordReg('sds123')); //false
邮箱匹配
名称允许汉字、字母、数字,下划线,中划线,域名可以有数字、字母、下划线、中划线组成
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.emailReg('[email protected]')); //true
console.log(easyRegexp.emailReg('sdfl!&*@qq.com')); //false
手机号匹配
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.phoneReg('18973682949')); //true
console.log(easyRegexp.phoneReg('14374927530')); //false
二代身份证匹配
若匹配不成功返回false,匹配成功则从身份证中提取出生年月日
示例
const easyRegexp = require('easy-regexp');
if(easyRegexp.IDReg('441880199903060224')) {
let [year,month,date] = easyRegexp.IDReg('441880199922060224');
console.log(year); //1999
console.log(month); //03
console.log(date); //06
}
else {
console.log("身份证号码匹配失败!")
}
html标签匹配
匹配并提取html标签的属性及对应属性值,输出为对象 注意:简写属性也可以提取成功!
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.htmlAttrReg('<img alt="" src="static/logo.png"/>')); //Object { alt: "", src: "static/logo.png" }
console.log(easyRegexp.htmlAttrReg('<input name="input" disabled/>')); //Object { name: "input", disabled: "true" }
16进制颜色匹配
可支持三位十六进制颜色缩写
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.colorReg('#efefef')); //true
console.log(easyRegexp.colorReg('#eee')); //true
url匹配
匹配url并依次输出协议、域名、端口、路径、请求参数(输出undefined即该项为空)
示例
const easyRegexp = require('easy-regexp');
console.log(easyRegexp.urlReg('https://www.baidu.com:8080/home/api?id=123&index=35')); //Array(5) [ "https", "www.baidu.com", "8080", "/home/api", Object { id: "123", index: "35" } ]
console.log(easyRegexp.urlReg('https://www.baidu.com/home/api?id=123&index=35')); //Array(5) [ "https", "www.baidu.com", undefined, "/home/api", Object { id: "123", index: "35" } ]