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})/,"The $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}$/; / / outputtrue
console.log(ref.test("Xiao Yao"));
Copy the code
5. Password strength is regular
Const reg = /^.*(?) const reg = /^.*(?) const reg = /^.*(? (=. {6})? =.*\d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @# $% ^ & *? ] ). * $/;/ / outputtrue
console.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+$/; / / outputtrue
console.log(posReg.test("42")); / / outputtrue
console.log(negReg.test("- 42")); / / outputtrue
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+$/; console.log(posReg.test("42.2"));
console.log(negReg.test("42.2"));
console.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}) $/; / / outputtrue
console.log(reg.test(" [email protected]"));
Copy the code
9. Regular URL
// const reg= /^((HTTPS? |ftp|file):\/\/)? ([\ \. Da - z -] +) \. ([a-z \.] {2, 6}) (/ \ \ / \ w - *) * \ /? $/; / / outputtrue
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]?) $/; / / outputtrue
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 /; / / outputtrue
console.log(reg.test("2017-05-11")); / / outputtrue
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) $/; / / outputtrue
console.log(reg.test("2017-02-11")); / / outputfalse
console.log(reg.test("2017-15-11")); / / outputfalse
console.log(reg.test("2017-02-29"));
Copy the code
12. QQ number regular
// const reg = /^[1-9][0-9]{4,10}$/; / / outputtrue
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) + $/; / / outputtrue
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}$/; / / outputtrue
console.log(reg.test("Sue B39006"));
Copy the code
15. Contains Chinese regulars
// Const reg = /[u4e00-u9fa5]/; / / outputtrue
console.log(reg.test("Xiao Yao"));
Copy the code
16. Match Chinese characters
const reg = /[\u4e00-\u9fa5]/gm
console.log(reg.test('Xiao Yao')
Copy the code
17. Match double-byte characters
const reg = /[^\x00-\xff]/igm
console.log(reg.test('1212')
Copy the code
- 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
Welcome to pay attention to the public account [Xiaoyao students]
Re-learn js series
Relearn JS JavaScript introduction
Relearn JS to use JavaScript in HTML
Data types => Data types
Relearn the basic JavaScript concepts of JS (middle) => operator
Relearn the basic JavaScript concepts of JS (part 2) => operator
Relearn JavaScript variables, scope, and memory issues in JS
ES6 Introduction series
ES6 Introduction to let and cont
ES6 introduction to variable deconstruction assignment
ES6 starter string extension
ES6 – An extension to get started with re
ES6 introduction to numerical extension
Extension of ES6 introductory functions
ES6 introduction to the array extension
Extensions to ES6 starter objects
New methods for ES6 entry objects
ES6 Symbol for beginners
Git tutorial
Front-end Git basics tutorial