Regular expressions improve the efficiency of the Code __ regular expressions

Source: Internet
Author: User
This post finally by the end of the Tianya prawns in 2017-3-23 14:30 edit

Regular expressions, a very old and powerful text-processing tool, can quickly implement a very complex business logic with a very short expression statement. Mastering regular expressions skillfully can greatly improve your development efficiency.
Regular expressions are often used for validation of fields or arbitrary strings, such as the following JavaScript code that validates the base date format: var reg =/^ (\\d{1,4}) (-|\\/) (\\d{1,2}) \\2 (\\d{1,2}) $/;
var r = Fieldvalue.match (reg);
if (r==null) alert (' Date format error! '); Copy code The following are the 20 regular expressions that are often used in the front-end development to be collated by the craftsman.

1. Verify password strength The strength of the password must be a combination of uppercase and lowercase letters and numbers, and cannot use special characters, which are between 8-10 lengths. ^ (? =.*\\d) (? =.*[a-z]) (? =.*[a-z]). {8,10}$ Copy Code

2. Verify that Chinese strings can only be Chinese. ^[\\u4e00-\\u9fa5]{0,}$ Copy Code

3. A string of numbers, 26 English letters, or underscores
^\\w+$ Copy Code

4. Check the e-mail address with the same password, the following is the e-mail address compliance regular Check statement. [\\w!#$%& ' *+/=?^_ ' {|} ~-]+(?:\ \. [\\w!#$%& ' *+/=?^_ ' {|} ~-]+) *@ (?: [\\w] (?: [\\w-]*[\\w]) +[\\w] (?: [\\w-]*[\\w])? Copy Code

5. Verify the ID number under the ID card number of the regular check. 15 or 18 bits. 15-bit: ^[1-9]\\d{7} ((0\\d) | ( 1[0-2])) (([0|1|2]\\d) |3[0-1]) \\d{3}$ copy code 18 bits: ^[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 Code

6. Check date "YYYY-MM-DD" format date check, has been considered the flat leap year. ^(?:(?! 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) $ copy Code

7. Check the amount of verification, accurate to 2 decimal places. ^[0-9]+ (. [ 0-9]{2})? $ copy Code

8. Check the phone number below is the domestic 13, 15, 18, the beginning of the mobile phone number regular expression. (Can be expanded according to the current domestic collection number of the first two-digit opening number) ^ (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 Code

9. To determine IE version of IE has not been completely replaced, many pages still need to do version compatibility, the following is the version of the Internet Explorer expression. ^.*msie [5-8] (?: \ \. [0-9]+]? (?!. *trident\\/[5-9]\\.0). *$ Copy Code

10. Verify the IP-V4 address IP4 regular statement. \\b (?:(? : 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]?) \\b Copy Code

11. Verify the IP-V6 address IP6 regular statement. (([0-9a-fa-f]{1,4}:) {7,7}[0-9a-fa-f]{1,4}| ( [0-9a-fa-f] {1,4}:) {1,7}:| ([0-9a-fa-f]{1,4}:) {1,6}:[0-9a-fa-f]{1,4}| ( [0-9a-fa-f] {1,4}:) {1,5} (: [0-9a-fa-f]{1,4}) {1,2}| ([0-9a-fa-f]{1,4}:) {1,4} (: [0-9a-fa-f]{1,4}) {1,3}| ( [0-9a-fa-f] {1,4}:) {1,3} (: [0-9a-fa-f]{1,4}) {1,4}| ([0-9a-fa-f]{1,4}:) {1,2} (: [0-9a-fa-f]{1,4}) {1,5}|[ 0-9a-fa-f]{1,4}:((: [0-9a-fa-f]{1,4}) {1,6}) |:( (: [0-9a-fa-f]{1,4}) {1,7}|:) | FE80: (: [0-9a-fa-f]{0,4}) {0,4}%[0-9a-za-z]{1,}|::(FFFF (: 0{1,4}) {0,1}:) {0,1} (25[0-5]| ( 2[0-4]|1{0,1}[0-9]) ({0,1}[0-9]) \.) {3,3} (25[0-5]| (2[0-4]|1{0,1}[0-9]) {0,1} [0-9]) | ([0-9a-fa-f]{1,4}:) {1,4}:((25[0-5]| ( 2[0-4]|1{0,1}[0-9]) ({0,1}[0-9]) \.) {3,3} (25[0-5]| (2[0-4]|1{0,1}[0-9]) {0,1} [0-9]) copy code

12. Check URL prefix application development many times the need to distinguish between the request is HTTPS or HTTP, through the following expression can take out a URL prefix and then logically judge. if (!s.match (/^[a-za-z]+:\\/\\//)) {s = ' http://' + s;} Copy Code

13. Extract URL link below This expression can filter out a paragraph of text in the URL. ^ (F|HT) {1} (TP|TPS): \\/\\/([\\w-]+\\.) +[\\w-]+ (\\/[\\w-/?%&=]*)? Copy Code

14. File path and extended prestigious school verification verify the file path and extension under Windows (. txt file in the following example) ^ ([a-za-z]\\:|\\\\) \\\\ ([^\\\\]+\\\\) *[^\\/:*? " <>|] +\\.txt (l)? $ copy Code


15. Extract color Hex codes Sometimes you need to extract the colour code from a Web page, you can use the following expression. ^# ([a-fa-f0-9]{6}| [A-fa-f0-9] {3}) $ copy Code

16. Extract the image of the Web page if you want to extract all the image information on the page, you can use the following expression. \\< *[IMG][^\\\\&GT;]*[SRC] *= *[\\ "\"]{0,1} ([^\\ "\ \ \ >]*) Copy code

17. Extract the hyperlinks in the HTML from the page hyperlinks. (<a\\s* (?!. *\\brel=) [^>]*) (href= "https?:\ \/\\/)((?! (:(?: www\\.)? '. Implode (' |: www\\.)? ', $follow _list) [^"]+)"((?!. *\\brel=) [^>]*] (?: [^>]*) > Copy Code

18. Find CSS properties by using the following expression, you can search for a matching CSS property. ^\\s*[a-za-z\\-]+\\s*[:]{1}\\s[a-za-z0-9\\s.#]+[;] {1} Copying code

19. Extract Note If you need to remove the annotation in the HMTL, you can use the following expression. <!--(. *?) --> Copy Code

20. Matching HTML tags you can match the label properties in HTML with the following expression. <\\/?\\w+ ((\\s+\\w+) (\\s*=\\s* (?:). *? "| '. *?'| [\\^ ' ">\\s]+)") +\\s*|\\s*) \\/?> Copy Code
Original link: http://www.jianshu.com/p/e7bb97218946

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.