Public account: wechat search front end tool person; Take more dry goods

  1. Regular expression instances are frequently used in services to facilitate future search and reduce workload.
  2. Many of them are used in their own development; Does not conform to the logic of all people; Or wrong; Welcome to point out mistakes

1. Only Chinese and English characters are allowed

let reg = /[\u4e00-\u9fa5_a-zA-Z]/

Copy the code

2. Only in Chinese, such as Chinese user name

// The string can only be Chinese.



let reg = /^[\\u4e00-\\u9fa5]{0,}$/;

Copy the code

3. Only uppercase letters and digits are allowed

let /^[A-Z0-9]$/;

Copy the code

4. The value contains 26 letters, digits, and underscores (_)

let reg = /^\\w+$/;

Copy the code

5. Verify password strength

// The password must contain a combination of uppercase and lowercase letters and numbers. No special characters can be used. The password length is between 8 and 10 characters.



letreg = /^(? =.*\\d)(? =.*[a-z])(? =. * [a-z]). 8, 10 {} $/;

// The password must contain two or more combinations of uppercase and lowercase letters, digits, and special characters. The password must be between 8 and 10 in length.



letreg = /^(((? =. * ([0-9])? =.*[a-zA-Z])|(? =. * ([0-9])? =.*[^\s0-9a-zA-Z])|(? =.*[a-zA-Z])(? =.*[^\s0-9a-zA-Z]))[^\s]+)$/;



Copy the code

6. Verify the E-mail address

As with passwords, here is a regular check for E-mail address compliance.

let reg = /[\\w!# $% & '* + / =? ^ _ ` {|} ~ -] + (? :\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(? :[\\w](? :[\\w-]*[\\w])? \ \.) +[\\w](? :[\\w-]*[\\w])? /;

Copy the code

7. Verify your ID card number

The following is the regular verification of the ID number. 15 or 18.

/ / 15:



let reg = /^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/;



/ / 18:



let reg = /^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$/;

Copy the code

8. Check your phone number

// All but 12

letReg = / ^ [1] [3,4,5,6,7,8,9] [0-9] {9}



// Mobile phone numbers starting with 13, 15, and 18. (The first two digits can be extended according to the current domestic collection number)



let reg = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/;

Copy the code

9. Check amount

1 / / way

letreg = ! / ^ \ d {1,} [.] (\ d {1, 2})? $/

// Mode 2 is accurate to 2 decimal places.

letreg = /^[0-9]+(.[0-9]{2})? $/;

Copy the code

10. Longitude and latitude regularity

/^[\u4e00-\u9fa5a-zA-Z]+$/

Copy the code

11. Verify the basic date format

letreg1 = /^\d{4}(\-|\/|\.) 1 \ \ d {1, 2} \ d {1, 2} $/;

 

letreg2 = /^(^(\d{4}|\d{2})(\-|\/|\.) \ d {1, 2} \ \ d {1, 2} $3) | (^ \ d \ d {1, 2} {4} years on \ d {1, 2} $) $/;

Copy the code

12. Calibration date

// Date verification in "YYYY-MM-DD" format, with flat leap years taken into account.

letreg = /^(? : (? ! 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) $/;

Copy the code

13. Check the VERSION of Internet Explorer

'IE' has not been completely replaced, many pages still need to do version compatibility, the following is the expression of the IE version check.



letreg = /^.*MSIE [5-8](? : \ \. [0-9] +)? (? ! .*Trident\\\/[5-9]\\.0).*$/;

Copy the code

14. Verify IP address V4

letreg = /\\b(? : (? : 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]?) \\b/;

Copy the code

15. Verify the IP-v6 address

letreg = / (([0-9 a - fA - F] {1, 4} {7, 7}) [0-9 a - fA - F] {1, 4} | ([0-9 a - fA - F] {1, 4} {1, 7}) : | ([0-9 a - fA - F] {1, 4} {1, 6}) : [0-9 a - fA - F] {1, 4} | ([0-9 a - fA - F] {1, 4} {1, 5}) (: [0-9 a - fA - F] {1, 4}, {1, 2} | ([0-9 a - fA - F] {1, 4} {1, 4}) (: [0-9 a - fA - F] {1, 4}, {1, 3} | ([0-9 a - fA - F] {1, 4} {1, 3}) (: [0-9 a - fA - F] {1, 4} {1, 4} | ([0-9] a - fA - F {1, 4}} {1, 2) (: [0-9 a - fA - F] {1, 4}, {1, 5} | [0-9 a - fA - F] {1, 4} : ((: [0-9 a - fA - F] {1, 4}, {1, 6}) | : ((: [0-9 a - fA - F] {1, 4} {1, 7} | :) | fe80: : [0-9 a - fA - F] {0, 4}) {0, 4} % [0-9 a zA - Z] {1} | : : (FFFF (: 0 {1, 4}) {0, 1} {0, 1} ((25) [0 to 5) | (2 [0 to 4] | 1 {0, 1} [ 0-9] [0-9] {0, 1}) \ \.) {3} (25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) | ([0-9 a - fA - F] {1, 4} {1, 4}) : ((25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) \ \.) {3} (25 [0 to 5) | (2 | 1 [0-4] [0-9] {0, 1}) [0-9] {0, 1})) /;

Copy the code

16. Get the URL? After carrying reference

let params={}

location.search.replace(/([^?&=]+)=([^&]+)/g, (_,k,v) => parmas[k] = v); 

cosnole.log(params) // ? a=b&c=d&e=f => {a:"b", c: "d", e: "f"}



Copy the code

17. Check the URL prefix

In application development, it is often necessary to distinguish between HTTPS and HTTP requests. The following expression can be used to extract a URL prefix and then determine logically.

if(! s.match(/^[a-zA-Z]+:\/\//)) {

   s = 'http://' + s;

}

Copy the code

18. Extract URL links

The following expression filters out a URL from a piece of text.

letreg = /^(f|ht){1}(tp|tps):\/\/([\w-]+\.) +[\w-]+(\/[\w- ./?%&=]*)? /;

Copy the code

19. Verify file path and extension

Verify the path and extension of the Windows file (.txt in this example)

let reg = /^([a-zA-Z]\\:|\\\\)\\\\([^\\]+\\)*[^\\/:*?"<>|]+\\.txt(l)? $/;

Copy the code

20. Extract Color Hex Codes

Sometimes you need to extract a color code from a web page, you can use the following expression.

let reg = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;

Copy the code

21 Extract the web page image

If you want to extract all the image information in a web page, you can use the following expression.

let reg = /\\< *[img][^\\>]*[src] *= *[\\"\'] {0, 1} ([^ \ \ \ "'\ >] *) /;

Copy the code

22. Extract page hyperlinks

Extract hyperlinks in HTML.

letreg = /(<a\\s*(? ! .*\\brel=)[^>]*)(href="https? ((: \ \ /)? ! (? : (? :www\\.) ? '.implode('|(? :www\\.) ? ',$follow_list). ')) [^"] +)"((? ! .*\\brel=)[^>]*)(? : [^ >] *) > /;

Copy the code

23 Search for the CSS properties

You can search for matching CSS properties using the following expression.

let reg = /^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#] + [;] {1} /;

Copy the code

24. Extract comments

If you need to remove comments from HMTL, you can use the following expression.

letreg = /<! - (. *?) -- > /;

Copy the code

25. The end

Article source: your blog at https://www.cnblogs.com/ljx20180807/p/9492047.html