侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

常用JavaScript正则表达式汇编与示例详解

前端  /  管理员 发布于 2年前   178

1.1 前言

目前收集整理了21个常用的javaScript正则表达式,其中包括用户名、密码强度、整数、数字、电子邮件地址(Email)、手机号码、身份证号、URL地址、 IP地址、 十六进制颜色、 日期、 微信号、车牌号、中文正则等。表单验证处理必备,赶紧收藏吧!

还会陆续加入新的正则进来,大家多提宝贵意见!

2.1 用户名正则

2.1.1 基本用户名正则

在做用户注册时,都会用到用户名正则校验。

定义基本用户名命名规则如下:

  1. 最短4位,最长16位 {4,16}
  2. 可以包含小写大母 [a-z] 和大写字母 [A-Z]
  3. 可以包含数字 [0-9]
  4. 可以包含下划线 [ _ ] 和减号 [ - ]
  5. 首字母只能是大小写字母

var pattern = /^[a-zA-Z][a-zA-Z0-9_-]{3,15}$/;//输出 trueconsole.log("ifat3 : "+pattern.test('ifat3'));//输出 trueconsole.log("Ifat3 : "+pattern.test('Ifat3'));//输出 trueconsole.log("ke30 : "+pattern.test('ke30'));//输出 falseconsole.log("30ke : "+pattern.test('30ke'));//输出 falseconsole.log("ke3 : "+pattern.test('ke3'));输出 falseconsole.log("ke30@ : "+pattern.test('ke30@'));//输出 falseconsole.log("ke30ke30ke30ke30ke30 : "+pattern.test('ke30ke30ke30ke30ke30'));

2.1.2 中文用户名正则

如果规则中加入允许中文用户名,则变更正则表达式如下:

var pattern = /^[a-zA-Z\u4E00-\u9FA5][a-zA-Z0-9\u4E00-\u9FA5_-]{3,15}$/;//输出 trueconsole.log("ifat3 : "+pattern.test('ifat3'));//输出 trueconsole.log("Ifat3 : "+pattern.test('Ifat3'));//输出 trueconsole.log("三十课毛瑞 : "+pattern.test('三十课毛瑞'));//输出 falseconsole.log("30ke : "+pattern.test('30ke'));//输出 falseconsole.log("ke3 : "+pattern.test('ke3'));//输出 falseconsole.log("ke30@ : "+pattern.test('ke30@')); //输出 falseconsole.log("ke30ke30ke30ke30ke30 : "+pattern.test('ke30ke30ke30ke30ke30'));

其中[\u4E00-\u9FA5]是汉字的正则匹配,包括基本汉字2万多个,其中\u4E00表示汉字“一”,具体请参见《汉字unicode编码范围》。

2.2 密码强度正则

//密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符var pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;//输出 trueconsole.log("iFat3#:"+pPattern.test("iFat3#"));

上述正则表达式只能对用户密码强度进行基本的通过性判定,关于密码强度验证更多的内容可参见:基于规则评分的密码强度检测算法分析及实现。

2.3 数字相关正则

2.3.1 整数正则

//正整数正则var posPattern = /^\d+$/;//负整数正则var negPattern = /^-\d+$/;//整数正则var intPattern = /^-?\d+$/;//输出 trueconsole.log("30:"+posPattern.test("30"));//输出 trueconsole.log("-30:"+negPattern.test("-30"));//输出 trueconsole.log("-30:"+intPattern.test("-30"));

2.3.2 浮点数正则

//正浮点数正则var posPattern = /^\d*\.\d+$/;//负浮点数正则var negPattern = /^-\d*\.\d+$/;//两位小数正则var twoPattern = /^-?\d*\.\d{2}$/; //输出 trueconsole.log("30.2:"+posPattern.test("30.2"));//输出 true console.log("-30.2:"+negPattern.test("-30.2"));//输出 true console.log("-30.22:"+twoPattern.test("-30.22"));

2.3.3 整数浮点数正则

可以是整数也可以是浮点数

//正数正则var posPattern = /^\d*\.?\d+$/;//负数正则var negPattern = /^-\d*\.?\d+$/;//数字正则var numPattern = /^-?\d*\.?\d+$/;//输出 trueconsole.log("30.2:"+posPattern.test("30.2"));//输出 true console.log("-30.2:"+negPattern.test("-30.2"));//输出 true console.log("-30.2:"+numPattern.test("-30.2"));

2.4 日期正则

2.4.1 出生日期正则

var pattern = /^((19[2-9]\d{1})|(20((0[0-9])|(1[0-8]))))\-((0?[1-9])|(1[0-2]))\-((0?[1-9])|([1-2][0-9])|30|31)$/;//输出 trueconsole.log(pattern.test("1923-3-18"));//输出 trueconsole.log(pattern.test("1923-4-31"));//输出 trueconsole.log(pattern.test("1923-2-29"));//输出 trueconsole.log(pattern.test("2016-2-29"));

上述正则验证还不完善,主要是2,4,6,9,11月份的天数问题。

2.4.2 通用日期正则

//日期正则,复杂判定var dP2 = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;//输出 trueconsole.log(dP2.test("2017-02-11"));//输出 falseconsole.log(dP2.test("2017-15-11"));//输出 falseconsole.log(dP2.test("2017-02-29"));

2.5 Email正则

2.5.1 基本Email正则

var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;//输出 trueconsole.log(pattern.test('cn30ke@163.com'));//输出 trueconsole.log(pattern.test('ifat3@sina.com.cn'));//输出 trueconsole.log(pattern.test('ifat3.it@163.com'));//输出 trueconsole.log(pattern.test('ifat3_-.@30ke.cn'));//输出 falseconsole.log(pattern.test('ifat3@30ke.online'));//输出 falseconsole.log(pattern.test('毛瑞@30ke.cn'));

基本Email正则是最常用的验证方式,也适合大多数的应用场景。从以上测试可以看出,该表达式不支持.online及.store结尾的域名。如需兼容这类域名(大于4位),调整正则结尾{2,4}的限制部分即可(例:{2,8})。另一个问题是Email用户名不能包括中文。

2.5.2 中文名Email正则

根据前一正则中的问题,追加两条规则如下:

用户名可以包括中文 [\u4e00-\u9fa5]
域名结尾最长可为8位 {2,8}

var pattern = /^([A-Za-z0-9_\-\.\u4e00-\u9fa5])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/;//输出 trueconsole.log(pattern.test('cn30ke@163.com'));//输出 trueconsole.log(pattern.test('ifat3@sina.com.cn'));//输出 trueconsole.log(pattern.test('ifat3.it@163.com'));//输出 trueconsole.log(pattern.test('ifat3_-.@30ke.cn'));//输出 trueconsole.log(pattern.test('ifat3@30ke.online'));//输出 trueconsole.log(pattern.test('毛瑞@30ke.cn'));

2.5.3 特定域名Email正则

在手机验证码出现之前,差不多邮箱验证是保证用户唯一性的唯一条件。而临时邮箱(也称10分钟邮箱或一次性邮箱)的出现,则使得邮箱验证及帐户激活这种机制失去了意义。而临时邮箱的地址是不可枚举的,我们只能才采取白名单的方式,只允许有限的邮箱域名通过验证。

var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|30ke.cn)$/;//输出 trueconsole.log(pattern.test('cn30ke@163.com'));//输出 falseconsole.log(pattern.test('ifat3@sina.com.cn'));//输出 trueconsole.log(pattern.test('ifat3.it@163.com'));//输出 trueconsole.log(pattern.test('ifat3_-.@30ke.cn'));//输出 falseconsole.log(pattern.test('ifat3@30ke.online'));//输出 falseconsole.log(pattern.test('毛瑞@30ke.cn'));

此方法虽然能保证验证安全性,但是如果白名单太长会造成模式字符串太长。这时可以将邮箱域名白名单写成数组,利用正则表达式做初步验证,用白名单做域名的二次验证。

常用域名白名单数组:

var domains= ["qq.com","163.com","vip.163.com","263.net","yeah.net","sohu.com","sina.cn","sina.com","eyou.com","gmail.com","hotmail.com"];

上述白名单只列举了常用的11种邮箱域名,大家可以根据需要适当补充或删减。

2.6 手机号码正则

//手机号正则var mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;//输出 trueconsole.log(mPattern.test("18600000000"));

2.7 身份证号正则

//身份证号(18位)正则var cP = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;//输出 trueconsole.log(cP.test("11010519880605371X"));

上述正则只能对身份证号进行基本的通过性判定,关于公民身份号码判定的更多内容可参见文档:公民身份号码正确性判定及程序实现

2.8 URL正则

//URL正则var urlP= /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;//输出 trueconsole.log(urlP.test(http://30ke.cn));

2.9 IP地址

2.9.1 IPv4地址正则

//ipv4地址正则var ipP = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;//输出 trueconsole.log(ipP.test("115.28.47.26"));

2.9.2 IPv6地址正则

//IPV6正则var pattern = /(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/;//输出 trueconsole.log(pattern.test("fe80:0000:0000:0000:0204:61ff:fe9d:f156"));

2.10 十六进制颜色正则

//RGB Hex颜色正则var cPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;//输出 trueconsole.log(cPattern.test("#b8b8b8"));

2.11 QQ号码正则

//QQ号正则,5至11位var qqPattern = /^[1-9][0-9]{4,10}$/;//输出 trueconsole.log(qqPattern.test("65974040"));

2.12 微信号正则

//微信号正则,6至20位,以字母开头,字母,数字,减号,下划线var wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;//输出 trueconsole.log(wxPattern.test("RuilongMao"));

2.13 车牌号正则

//车牌号正则var cPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;//输出 trueconsole.log(cPattern.test("京K39006"));

2.14 包含中文正则

//包含中文正则var cnPattern = /[\u4E00-\u9FA5]/;//输出 trueconsole.log(cnPattern.test("30课"));

总结

以上所述是小编给大家介绍的常用JavaScript正则表达式汇编与示例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!


  • 上一条:
    perl特殊符号及默认的内部变量
    下一条:
    完美解决webstorm启动索引文件卡死的问题
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在js中使用URL类用来解析处理URL的示例代码(0个评论)
    • js中动画事件:requestAnimationFrame、transitionend、animation...(0个评论)
    • 在js中mouseover和 mouseenter的区别浅析(0个评论)
    • uniapp调用手机实现打电话录音功能示例代码(0个评论)
    • 近期文章
    • windows系统中安装FFMpeg及在phpstudy环境php7.3 + php-ffmpeg扩展的使用流程步骤(0个评论)
    • 在go语言中对浮点的数组、切片(slice)进行正向排序和反向排序(0个评论)
    • 在go语言中对整数数组、切片(slice)进行排序和反向排序(0个评论)
    • 在go语言中对字符串数组、切片(slice)进行排序和反向排序(0个评论)
    • 最新国内免注册ChatGPT体验站_ChatGPT镜像站访问链接地址2023/3/28持续更新(0个评论)
    • 在Laravel项目中的实现无密码认证之:发送邮箱链接授权(0个评论)
    • 在go语言中使用GoRoutines实现高性能并发批量调用api示例(0个评论)
    • Docker撤回受争议的收费方案,又可以继续使用docker了(0个评论)
    • 在go语言生成唯一ID之SnowFlake算法(0个评论)
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客