Git address

Regular expressions are commonly used at the front end

1. Verify id card

const reg =/(^[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]$)|(^[1-9]\d{5}\d{2}((0[1- 9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/; console.log(reg.test(320874199709084732))Copy the code

2. Obtain date of birth according to id card

function getBirthdayFromIdCard(idCard) { var birthday = ""; if(idCard ! = null && idCard ! = ""){if(idcard.length == 15){birthday = "19"+ idcard.substr (6,6); } else if(idcard.length == 18){birthday = idcard.substr (6,8); } birthday = birthday.replace(/(.{4})(.{2})/,"$1-$2-"); } return birthday; }Copy the code

3. Verify mobile phone rules

const reg = /^1[3456789]\d{9}$/;
console.log(reg.test(13879763331))

Copy the code

4. The user name is regular

Const reg = /^[a-za-z0-9_ -]{4,16}$/; // Output true console.log(ref.test); // Output true console.log(ref.test);Copy the code

5. Password strength is regular

Const reg = /^.*(?) const reg = /^.*(?) const reg = /^.*(? (=. {6})? =.*\d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @ # $% ^ & *?] ). * $/; / / output true on the console. The log (" = = "+ reg. Test (" infopush#"));Copy the code

6. Integer regulars

// Positive integer regular const posReg = /^\d+$/; // Negative integer regular const negReg = /^-\d+$/; // Integer regular const numReg = /^-? \d+$/; // Output true console.log(posreg.test ("42")); // Print true console.log(negreg.test ("-42")); // Output true console.log(numreg. test("-42"));Copy the code

7. Digital re

// Positive regular const posReg = /^\d*\.? \d+$/; // Negative regular const negReg = /^-\d*\.? \d+$/; // numReg = /^-? \d*\.? \d+$/; The console. The log (posReg. Test (" 42.2 ")); The console. The log (negReg. Test (" 42.2 ")); The console. The log (numReg. Test (" 42.2 "));Copy the code

8. Email regular

/ / Email regular const reg = / ^ ([A - Za - z0-9 _ \ - \]) + \ @ ([A - Za - z0-9 _ \ - \]) + \. ([A Za - z] {2, 4}) $/; // Print true console.log(reg.test(" [email protected]"));Copy the code

9. Regular URL

// const reg= /^((HTTPS? |ftp|file):\/\/)? ([\ \. Da - z -] +) \. ([a-z \.] {2, 6}) (/ \ \ / \ w - *) * \ /? $/; // Print true console.log(reg.test("http://lieme.cn"));Copy the code

10. The IPv4 address is regular

Const reg = /^(? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) $/; // Print true console.log(reg.test("115.28.47.26"));Copy the code

11. Date is regular

/ / date regular, simple, do not make month and date for determining const reg = / ^ \ d {4} (\) \ d {1, 2} \ \ d {1, 2} $1 /; // Print true console.log(reg.test("2017-05-11")); // Output true console.log(reg.test("2017-15-11")); Const reg = /^(? : (? ! 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(reg.test("2017-02-11")); // Print false console.log(reg.test("2017-15-11")); // Print false console.log(reg.test("2017-02-29"));Copy the code

12. QQ number regular

// const reg = /^[1-9][0-9]{4,10}$/; // Print true console.log(reg.test("11111"));Copy the code

13. Micro signal regularization

/ / WeChat ID regular, 6 to 20, begin with a letter, letters, Numbers and minus sign, underline const reg = / ^ [a zA - Z] ([9] - _a - zA - Z0 - {5} 3) + $/; // Print true console.log(reg.test("infopush"));Copy the code

14. Regular license plate number

// const reg = /^[a-z]{1}[a-z]{1}[A-z]{1}[A-z]{1}[A-z0-9]{4}[a-z0-9) {1}$/; // Print true console.log(reg.test(" sub39006 "));Copy the code

15. Contains Chinese regulars

// Const reg = /[u4e00-u9fa5]/; // Output true console.log(reg.test(" reg.test "));Copy the code

16. Match Chinese characters

Const reg = /[u4e00-u9fa5]/gm console.log(reg.test(' U4e00-u9fa5 '))Copy the code

17. Match double-byte characters

const reg = /[^\x00-\xff]/igm
console.log(reg.test('1212')
Copy the code
  1. Match the first margin
const reg = /(^\s*)|(\s*$)/
console.log(reg.test(' d ')
Copy the code

19. Numbers only

const reg = /^\d+$/
console.log(reg.test(112))
Copy the code

20. Enter a maximum of N digits

const reg = /^\d{n}$/
console.log(reg.test(11111))
Copy the code

21. Enter at least n digits

const reg = /^\d{n,}$/
console.log(reg.test(2222))
Copy the code

22. Enter only M to N digits

const reg = /^\d{m,n}$/
console.log(reg.test(1212))
Copy the code

23. Letters only

const reg = /^[a-z]+$/i
console.log(reg.rest('ddd'))
Copy the code

23. Letters must be uppercase or small

const reg = /^[A-Z]+$/
console.log(reg.test(SSS))

const reg = /^[a-z]+$/
console.log(reg.test(sss))
Copy the code

24. Only English and numbers can be used

const reg = /^[a-z0-9]+$/i
console.log(reg.test(333ffF))
Copy the code

25. Contains only digits and underscores (_)

const reg = /^\w+$/
console.log(reg.test(dd33_))
Copy the code

26. Positive or negative numbers with two decimal points

const reg =^(\-)? \ d + (\ \ d {1, 2})? $console. The log (reg. Test (11.11))Copy the code

27. To be added

Git tutorial

Front-end Git basics tutorial