JavaScript常用正则校验

1:匹配url

// [^\s*"$] 匹配结尾是引号的url
// [^\s]* 匹配一般url
let urlReg = /[A-z]+:\/\/[^\s*"$]*/
let str = ''
console.log(str.match(urlReg))
// [ 'http://www.baidu.com', index: 21, input: '', groups: undefined ]

2:匹配手机号

let mobileReg = /^1[3|4|5|7|8][0-9]\d{8}$/
let mobile = 13039393939
console.log(mobileReg.test(mobile)) // true

3:匹配电话号码

let telReg = /^(\d{3,4}-)\d{7,8}$/
let tel = '0310-8403185'
console.log(telReg.test(tel)); // true

4:账号密码

// 字母加数字,首位为字母
let accReg = /^[A-z]\w{5,15}$/
let acc = 'a10010'
console.log(accReg.test(acc)); // true

5:邮箱验证

let emailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
let email = '[email protected]'
console.log(emailReg.test(email)); // true

6:数字限制

let numReg = /^[0-9]*$/
let num = '1223131'
console.log(numReg.test(num)); // true

7:中文限制

let chineseReg = /^[\u4e00-\u9fa5]{0,}$/
let chinese = '只限制输入中文'
console.log(chineseReg.test(chinese)); // true

8:特殊符号验证

let _reg = /[%&',;-_=?$\^]+/
let _ = '222_aaa'
console.log(_reg.test(_)); // true

9:身份证号码

可以看下这个博主写的,非常详细也比较实用链接:  正则匹配身份证号码icon-default.png?t=M7J4http:// https://blog.csdn.net/fly_wugui/article/details/88902156

你可能感兴趣的:(前端,正则,javascript,开发语言,ecmascript)