SoFunction
Updated on 2025-03-08

15 commonly used js regular expressions such as username and password

15 commonly used javaScript regular expressions have been collected and sorted out, including username, password strength, integer, number, email address (Email), mobile phone number, ID number, URL address, IPv4 address, hexadecimal color, date, QQ number, WeChat account, license plate number, and Chinese regularity. A must-have for form verification processing.

Regular expressions are usually used for two tasks: 1. Verification, 2. Search/Replace. When used for verification, it is usually necessary to add it separately before and after.^and$, to match the entire string to be verified; whether to add this limitation when searching/replacement depends on the search requirements, and it is also possible to add \b instead of ^ and $ before and after. Except for a few, no restrictions are added before and after the common regular expressions listed in this table. Please handle them yourself as needed.

1 Username Regular

//Username regular, 4 to 16 digits (letters, numbers, underscores, minus signs)var uPattern = /^[a-zA-Z0-9_-]{4,16}$/;
//Output true(("iFat3"));

2 Password strength regularity

// Password strength regular, at least 6 digits, including at least 1 uppercase letter, 1 lowercase letter, 1 digit, 1 special charactervar pPattern = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
//Output true("=="+("iFat3#"));

3 integer regularity

//Positive integer regularityvar posPattern = /^\d+$/;
//Negative integer regularityvar negPattern = /^-\d+$/;
//Integer Regularvar intPattern = /^-?\d+$/;
//Output true(("42"));
//Output true(("-42"));
//Output true(("-42"));

4 Number regularity

It can be an integer or a floating point number

//Positive number regularityvar posPattern = /^\d*\.?\d+$/;
//Negative number positivevar negPattern = /^-\d*\.?\d+$/;
//Number regularvar numPattern = /^-?\d*\.?\d+$/;
(("42.2"));
(("-42.2"));
(("-42.2"));

5 Email Regularity

//Email Regularvar ePattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
//Output true((65974040@));

Or use

^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$

After testing, the matching effect is good

6 Mobile phone number regular

//Mobile phone number regularvar mPattern = /^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/;
//Output true(("18600000000"));

7 ID number regular

//Identity card number (18-digit) regularvar 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]$/;
//Output true(("11010519880605371X"));

8 URL Regularity

//Identity card number (18-digit) regularvar 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]$/;
//Output true(("11010519880605371X"));

9 IPv4 address regularity

// ipv4 address regularvar 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]?)$/;
//Output true(("115.28.47.26"));

10 Hexadecimal color regularity

//RGB Hex color regularityvar cPattern = /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
//Output true(("#b8b8b8"));

11 Date Regularity

//Date regularity, simple judgment, no month and date judgment was madevar dP1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/;
//Output true(("2017-05-11"));
//Output true(("2017-15-11"));
//Date regularity, complex judgmentvar 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)$/;
//Output true(("2017-02-11"));
//Output false(("2017-15-11"));
//Output false(("2017-02-29"));

12 QQ number regularity

//QQ number regular, 5 to 11 digitsvar qqPattern = /^[1-9][0-9]{4,10}$/;
//Output true(("65974040"));

13 WeChat ID regularity

//WeChat ID regular, 6 to 20 digits, starting with letters, letters, numbers, minus signs, underscoresvar wxPattern = /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/;
//Output true(("RuilongMao"));

14 License plate number regular

//The license plate number is regularvar cPattern = /^[Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Hunan, Anhui, Shandong, New Jiangsu, Zhejiang, Jiangxi, Hubei, Guizhou, Gansu, Shanxi, *, Shaanxi, Ji, Fujian, Guangdong, Qinghai, Tibet, Sichuan, Ningqiong envoyA-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9Hangout police in * and Macao]{1}$/;
//Output true(("Beijing K39006"));

15 Contains Chinese rules

//Include Chinese rulesvar cnPattern = /[\u4E00-\u9FA5]/;
//Output true(("42 degrees"));