前端最常用的25个正则表达式,代码效率提高 80%

在日常开发中,正则表达式是非常有用的,正则表达式在每个语言中都是可以使用的,他就跟JSON一样,是通用的。在日常开发中,了解一些常用的正则表达式,能大大提高你的工作效率,例如

字符串的匹配

表单项的格式校验

今天就给大家分享25个开发中常用的正则表达式吧!!!希望大家能提高代码效率!!!

1、手机号码的校验
constphoneReg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/constphoneStr1=''((phoneStr1))//trueconstphoneStr2='897'((phoneStr2))//false
2、身份证的校验
constsfzReg=/^[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]$/constsfzStr1='4012130'((sfzStr1))//trueconstsfzStr2='7212183'((sfzStr2))//false
3、邮箱的校验
constemailReg=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/constemailStrWY='956666@163.com'//163邮箱constemailStrQQ='956666@'//qq邮箱((emailStrWY))//((emailStrQQ))//trueconstnoEmail='72873213.com'((noEmail))//false
4、URL的校验
consturlReg=/^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w\.-]*)*\/?$/consturlStr1=''((urlStr1))//trueconsturlStr2='sss:///xxx/xxx'((urlStr2))//false
5、IPv4的校验
constipv4Reg=/^(?:(?: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]?)$/constipv4Str1='122.12.56.65'((ipv4Str1))//trueconstipv4Str2='122.12.56.655'((ipv4Str2))//false
6、16进制颜色的校验
constcolor16Reg=/^fff'((color16Str1))//trueconstcolor16Str2='000;"spanstyle="color:000;"',//'',//index:5,//input:'divstyle="background:fff"/span/div',//groups:undefined//]((htmlStr))//[//'style="color:000;"spanstyle="color:([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})/gconsthtmlStr='divstyle="background:fff"/span/div'((htmlStr))//[//'000;"spanstyle="color:fff',//'fff',//index:49,//input:'divstyle="background:fff"/span/div',//groups:undefined//]
25、匹配htmlTag(html标签)
constReg=/("[^"]*"|'[^']*'|[^'"])*/gconsthtmlStr='divstyle="background:fff"/span/divh1/h1'((htmlStr))((htmlStr))((htmlStr))((htmlStr))((htmlStr))((htmlStr))
结语

如果你觉得此文对你有一丁点帮助,点个赞,鼓励一下

免责声明:本文章如果文章侵权,请联系我们处理,本站仅提供信息存储空间服务如因作品内容、版权和其他问题请于本站联系