1. Create regular expressions
1. Method 1
Regular expression literals are enclosed in a pair of slashes let re1 = /ABC\-001/;Copy the code
2. Method 2
Const re2 = new RegExp('ABC\\-001'); console.log(re1) // /ABC\-001/Copy the code
2. Regular expression identification
G Global (match multiple times) I case insensitive (ignore character case) m multi-line (^ and $can match line terminator)Copy the code
Regular expression characters
\d is equivalent to [0-9], matching a number \w is equivalent to [0-9a-z_A -z] can match a letter or number. Can match any character * for any character (including 0) + for at least one character? \d{3} matches three digits, for example, '010' {n,m} matches n-m characters ^ indicates the beginning of a line, ^\d indicates that it must start with a digit $indicates that it must end with a digit. \ Backslashes are used to escape special charactersCopy the code
Instance analysis
/\d{3}\s+\d{3,8}/ const my_regexp =/\d{3}\s+\d{3,8}/ Read from left to right: 1.\d{3} means to match 3 digits, such as '010'; 2.\s can match a space (including Tab), so \s+ means at least one space, such as' ', '\t\t', etc.; 3.\d{3,8} represents 3-8 numbers, such as '1234567'. [] indicates the range [0-9A-za-z \_], which can match a number, letter, or underscore. [0-9a-za-z \_]+ matches at least one string consisting of digits, letters, or underscores, such as 'a100', '0_Z', 'js2015', etc. [a-za-z \_\$][0-9a-za-z \_\$]* matches any string that starts with a letter or underscore and is followed by a number or letter or underscore and $, which is JavaScript allowed variable names. [A-za-z \_\$][0-9a-za-z \_\$]{0, 19} more precisely limits the length of a variable to 1-20 characters (the first character + the last maximum of 19 characters).Copy the code
4. Instance method
1. The test() method tests whether a given string meets the criteria (do not use the G identifier for this method)
Var/re = ^ \ d {3} \ \ d {3, 8} $/; re.test('010-12345'); // true re.test('010-1234x'); // false re.test('010 12345'); // falseCopy the code
2. The exec() method returns an Array after a successful match. The first element is the entire string matched by the regular expression.
Var re = /^(\d{3})-(\d{3,8})$/; re.exec('010-12345'); // ['010-12345', '010', '12345'] re.exec('010 12345'); // nullCopy the code
Regular Regular Handbook
1. China Mobile Phone Number
1, China mobile phone number (serious), according to the ministry of the latest mobile phone them roughly 2019 / ^ (86) (\ | + 00)? (1) (3 [\ d] to | |,6,7,9 [5] (4) (5 [0, 3, 5-9]) | (6 [5-7]) | (7-8 0 and 3) | (8 [\] d) | (9,8,9 [1])) \ d {8} $/ 2, China mobile phone number (loose), As long as it is 13,14,15,16,17,18,19 beginning can / ^ (86) (\ | + 00)? 1 [3-9] \ d {9} $/ 3, China mobile phone number (the most lenient), as long as it is 1 at the beginning, if your mobile phone number is used to receive messages, give priority to suggest choose this one / ^ (86) (\ | + 00)? 1\d{10}$/Copy the code
2, domestic landline telephone, such as: 0341-86091234
/\d{3}-\d{8}|\d{4}-\d{7}/
Copy the code
3. Email address
/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](? : [a - zA - Z0-9 -], 21 {0} [a zA - Z0-9])? (? :\.[a-zA-Z0-9](? : [a - zA - Z0-9 -], 21 {0} [a zA - Z0-9])?) * $/Copy the code
4. Bank card Number
/^([1-9]{1})(\d{15}|\d{18})$/
Copy the code
5. Id Number
1, generation id number (15 digit number) / ^ \ d {8} (0 \ | d | | 10 11, 12) (\ [0-2] d | | 30 31) \ d {3} $/ 2, the second generation id card (18), and the last is the check digit, may for digital or character X / ^ \ d {6} (18 19 20) | | \ d {2} (0 \ | d | | 10 11 12) (\ [0-2] d | | 30 31) \ d {3} (\ d | | X) X $/ 3, id number, Support for 1/2 generation (15 /18 digits) /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/Copy the code
6. Passport (including Hong Kong and Macao)
/(^[EeKkGgDdSsPpHh]\d{8}$)|(^(([Ee][a-fA-F])|([DdSsPp][Ee])|([Kk][Jj])|([Mm][Aa])|(1[45]))\d{7}$)/
Copy the code
7, website
/^((https? |ftp):\/\/)? ([\ da - z -] +) \. ([a-z.] {2, 6}) (\ / \ w \] *) * \ /? /Copy the code
8, whether the QQ number format is correct
/ ^ (1-9] [0-9] {4, 10} $/Copy the code
9. Chinese name
/ ^ ([the] \ u4e00 - \ u9fa5 16th {2}) $/Copy the code
10. English name
/ (^ {1} [a zA - Z] [a zA - Z \ s] {0, 20} [a zA - Z] {1} $) /Copy the code
11. Whether it consists of numbers and letters
/^[A-Za-z0-9]+$/
Copy the code
12. Pure English letters
/ ^ [a zA - Z] + $/ pure English lowercase letters / ^ [a-z] + $/ uppercase letters / ^ [a-z] + $/Copy the code
13. Pure Chinese/Chinese characters
/ ^ (? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\u D86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD 86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/Copy the code
14. Pure numbers
/^\d{1,}$/
Copy the code
China postcode
/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/
Copy the code
16, Contains Chinese characters and numbers only
/ ^ ((? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\u D86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD 86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])|(\d))+$/Copy the code
17. Cannot contain letters
/^[^A-Za-z]*$/
Copy the code
18. Hexadecimal colors
/ ^ #? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/Copy the code
17. Password verification
Strong: contains at least six characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character /^.*(? (=. {6})? =.*\d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @ # $% ^ & *?] *$/ : letters + digits, letters + special characters, digits + special characters /^(? ! [a-zA-z]+$)(? ! \d+$)(? ! [! @ # $% ^ & *] + $) [a zA - Z \ d! @ # $% ^ & *] + $/ weak: pure digital, pure letters, special characters / ^ (? :\d+|[a-zA-Z]+|[!@#$%^&*]+)$/Copy the code
18, whether HTML tag (loose match)
/ < > (. *). * < 1 > \ / \ | < (. *) \ / > /Copy the code
19, user name regular, 4 to 16 characters (letters, digits, underscores, minus signs)
/ ^ [a - zA - Z0 - _ - 9] $/ dec {4}Copy the code
20, wechat, 6 to 20 characters, beginning with a letter, letter, digit, minus sign, underscore
Var wxPattern = / ^ [a zA - Z] ([- _a - zA - Z0-9] {5} 3) + $/; // Print true console.log(wxpattern.test ("RuilongMao"));Copy the code
20, date,
var dP2 = /^(? : (? ! 0000) [0-9] {4} - (? : (? : 0 | [1-9] [0-2] 1) - (? : 0 [1-9] [0-9] | | 1 2 [0 to 8]) | (? : 0 [9] 13 - | [0-2] 1) - (? 30) : 29 | | (? : 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 console.log(dp2.test ("2017-02-11")); // Print false console.log(dp2.test ("2017-15-11"));Copy the code
21. User name
Var uPattern = /^[a-za-z0-9_ -]{4,16}$/; var uPattern = /^[A-za-z0-9_ -]{4,16}$/; // Print true console.log(upattern.test ("iFat3"));Copy the code
Social unified credit code verification
Copy the code
In actual combat
let phoneNumber = $('[name=phone]').val(); if (! PhoneNumber) {alert(" phoneNumber cannot be empty!" ); return false; } var phoneReg = /(1[3-9]\d{9}$)/; if (! Phonereg. test(phoneNumber)) {alert(" please enter your phoneNumber in the correct format!" ); return false; }Copy the code
Extract the Chinese
let textArr = text.match(/[\u4e00-\u9fa5]/g)
Copy the code
Only Chinese, English and Spaces can be entered in the input box
Const reg = /^[A-za-z \ u4E00-\ u9FA5 \s]+$/ < form. Item name="contentFirst" label=" Text area "rules={[{required: true }, { validator: (rule, value, cbk) => { if (! Reg.test (value)) {return CBK (' cannot input numbers or other special symbols ')} CBK ()},}, ]} > <Input allowClear maxLength={20} onChange={handleContentFirst} style={{ width: '100%' }} /> </Form.Item>Copy the code