15 common JavaScript regular expressions from geek headlines

Source: Internet
Author: User

Summary
Collected 15 commonly used JavaScript regular expressions, including user name, password strength, integer, number, email address (email), mobile phone number, social Security number, URL address, IPv4 address, hex color, date, QQ number, number, license plate number, Zhong Wenjing. Form validation processing Essentials, quickly collect it!

User name Regular
//用户名正则,4到16位(字母,数字,下划线,减号)varuPattern = /^[a-zA-Z0-9_-]{4,16}$/;//输出 trueconsole.log(uPattern.test("iFat3"));
Password Strength regularization
//密码强度正则,最少6位,包括至少1个大写字母,1个小写字母,1个数字,1个特殊字符varpPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[[email protected]#$%^&*? ]).*$/;//输出 trueconsole.log("=="+pPattern.test("iFat3#"));
Integer regular
//正整数正则varposPattern = /^\d+$/;//负整数正则var negPattern = /^-\d+$/;//整数正则varintPattern = /^-?\d+$/;//输出 trueconsole.log(posPattern.test("42"));//输出 trueconsole.log(negPattern.test("-42"));//输出 trueconsole.log(intPattern.test("-42"));
Number Regular
可以是整数也可以是浮点数//正数正则var posPattern = /^\d*\.?\d+$/;//负数正则var negPattern = /^-\d*\.?\d+$/;//数字正则varnumPattern = /^-?\d*\.?\d+$/;console.log(posPattern.test("42.2"));console.log(negPattern.test("-42.2"));console.log(numPattern.test("-42.2"));
Email regular
//Email正则varePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;//输出 trueconsole.log(ePattern.test("[email protected]"));
Mobile phone number Regular
//手机号正则varmPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;//输出 trueconsole.log(mPattern.test("18600000000"));
Regular ID number
//身份证号(18位)正则varcP = /^[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"));
URL Regular
//URL正则varurlP= /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;//输出 trueconsole.log(urlP.test("http://42du.cn"));
IPV4 Address Regular
//ipv4地址正则varipP = /^(?:(?: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"));
Hex Color Regular
//RGB Hex颜色正则varcPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;//输出 trueconsole.log(cPattern.test("#b8b8b8"));
Date Regular
//日期正则,简单判定,未做月份及日期的判定var dP1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/;//输出 trueconsole.log(dP1.test("2017-05-11"));//输出 trueconsole.log(dP1.test("2017-15-11"));//日期正则,复杂判定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"));
QQ Number Regular
//QQ号正则,5至11位varqqPattern = /^[1-9][0-9]{4,10}$/;//输出 trueconsole.log(qqPattern.test("65974040"));
The number is regular
//号正则,6至20位,以字母开头,字母,数字,减号,下划线varwxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;//输出 trueconsole.log(wxPattern.test("RuilongMao"));
Regular car Grade
//车牌号正则varcPattern = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;//输出 trueconsole.log(cPattern.test("京K39006"));
Include Zhong Wenjing
//包含中文正则varcnPattern = /[\u4E00-\u9FA5]/;//输出 trueconsole.log(cnPattern.test("42度"));

Original address: Http://funteas.com/topic/592f828e572bfb9c4d965ab0

15 common JavaScript regular expressions from geek headlines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.