Regular expressions, a very old and powerful text processing tool, can quickly implement a very complex business logic by simply using a very short expression statement. Mastering regular expressions skillfully can make your development efficiency greatly improved.
Regular expressions are often used for validation of fields or arbitrary strings, such as the following snippet of JavaScript code that verifies the basic date format:
var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2var r = fieldValue.match(reg); if(r==null)alert(‘Date format error!‘);
The following is a list of regular expressions used by the craftsmen in front- end development.
1. Check Password strength
The strength of the password must be a combination of uppercase and lowercase letters and numbers, no special characters, and a length of 8-10.
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
2. Verifying Chinese
The string can only be in Chinese.
^[\\u4e00-\\u9fa5]{0,}$
3. A string of numbers, 26 English letters, or underscores
^\\w+$
4. Verify the e-mail address
As with the password, here is the regular check statement for e-mail address compliance.
[\\w!#$%&‘*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&‘*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
5. Check the ID number
The following is a regular check of the ID number. 15 or 18 bits.
15-bit:
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$
18-bit:
^[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)$
6. Check the date
Date checksum in "YYYY-MM-DD" format, which has been considered for a 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[ Geneva])-31) | (?:[0-9]{2} (?: 0 [ -]| [2468][048]| [13579][ -])| (?: 0 [ -]| [2468][048]| [13579][ -]) 00)-02-29) $
7. Check the amount
The amount of the check, accurate to 2 decimal places.
^[0-9]+(.[0-9]{2})?$
8. Check the phone number
The following is the domestic mobile phone number 13, 15, 18, the regular expression.
^ (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 }$
9. Determine the version of IE
IE has not been completely replaced, many pages still need to do version compatibility, the following is the version of IE to check the expression.
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$
10. Verifying the IP-V4 address
IP4 the 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
11. Verifying the IP-V6 address
IP6 the 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}(( -[0-5]| (2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.) {3,3}( -[0-5]| (2[0-4]|1{0,1}[0-9]){0,1}[0-9])| ([0-9a-fa-f]{1,4}:){1,4}:(( -[0-5]| (2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.) {3,3}( -[0-5]| (2[0-4]|1{0,1}[0-9]){0,1}[0-9]))
12. Check the prefix of the URL
Many times in application development need to distinguish between the request is HTTPS or HTTP, the following expression can be taken out of a URL prefix and then logical judgment.
if (!s.match(/^[a-zA-Z]+:\\/\\//)){ ‘http://‘ + s;}
13. Extracting URL links
The following expression can filter out URLs in a text.
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?
14. File paths and extended schools
Verify file path and extension
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
15. Extract the color Hex Codes
Sometimes you need to extract the color code from the Web page, and you can use the following expression.
\\#([a-fA-F]|[0-9]){3,6}
16. Extract Web Images
If you want to extract all the image information from a webpage, you can use the following expression.
\\< *[img][^\\>]*[src] *= *[\\"\\‘]{0,1}([^\\"\\‘\\ >]*)
17. Extract Page Hyperlinks
Extracts hyperlinks in HTML.
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?://)((?!(?:(?:www\\.)?‘.implode(‘|(?:www\\.)?‘, $follow_list).‘))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
18. Refine CSS
The following expression allows you to search the CSS for the same property value to achieve the purpose of refining the code.
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
19. Extracting Annotations
If you need to remove a comment from HMTL, you can use the following expression.
<!--(.*?)-->
20. Matching HTML tags
The following expression can be used to match the label in the HTML.
</?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|‘.*?‘|[\\^‘">\\s]+))?)+\\s*|\\s*)/?>
Correlation syntax for regular expressions
Here is a very nice regular expression I found Cheat Sheet, which can be used to quickly find the relevant syntax.
Learning Regular Expressions
I saw on the internet a fairly good regular expression Quick learning guide, students interested in continuing in-depth study can be consulted.
Regular Expression Online test tool
Regex101 is a very good online test tool for regular expressions, you can test your regular expression online directly.
If you also have a collection of useful regular expressions, you can also share them in your reviews ^_^
The technical craftsmen, above content welcome everybody to share to the friend Circle/micro Bo and so on. If you want to reprint, please contact authorized by Jane. Thank you!
Knowing these 20 regular expressions allows you to write less than 1,000 lines of code