20 regular expressions and 20 Regular Expressions
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 can greatly improve your development efficiency.
Regular Expressions are often used for field or any string verification. For example, the following section verifies the JavaScript code of the basic 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!');
Below isTechnicianSorted, often used in front-end development20Regular Expressions.
1. Verify password strength
The password must be a combination of uppercase and lowercase letters and numbers. special characters are not allowed and the password length must be between 8 and 10.
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
2. Verify Chinese Characters
The string can only be Chinese.
^[\\u4e00-\\u9fa5]{0,}$
3. A string consisting of digits, 26 English letters, or underscores
^\\w+$
4. Verify the email address
Like passwords, the following is a regular check statement for E-mail address compliance.
[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
5. Verify the ID card number
Below is the regular verification of the ID card number. 15 or 18 digits.
15 digits:
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$
18 digits:
^[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. Verification date
The date verification in the format of "yyyy-mm-dd" has been taken into consideration.
^(?:(?!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)$
7. Verify the amount
Amount verification, accurate to 2 decimal places.
^[0-9]+(.[0-9]{2})?$
8. Verify the mobile phone number
The following is a regular expression for mobile phone numbers starting with "13", "15", and "18" in China. (According to the current domestic collection number, expand the first two numbers)
^(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 IE version
IE is not completely replaced yet. Many pages still need to be compatible with the version. The following is the expression for IE version check.
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$
10. Verify 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
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]))
12. Check the URL prefix.
In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. The following expression can be used to retrieve the prefix of a url and then perform logic judgment.
if (!s.match(/^[a-zA-Z]+:\\/\\//)){ s = 'http://' + s;}
13. Extract URL links
The following expression filters out the URLs in a piece of text.
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?
14. File Path and extension Verification
Verify the file names and extensions under Windows (The following example is the. txt file)
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$
15. extract Color Hex Codes
You can use the following expression to extract the color code from a webpage.
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
16. Extract webpage Images
If you want to extract all the image information on the webpage, you can use the following expression.
\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)
17. Extract the page hyperlink
Extracts hyperlinks from html.
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>
18. Search for CSS attributes
The following expression can be used to search for matching CSS attributes.
^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}
19. Extract comments
If you need to remove the comment in HMTL, you can use the following expression.
<!--(.*?)-->
20. Matching HTML tags
The following expression can match the tag attribute in HTML.
<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>
Regular expression syntax
Below is a very good Regular Expression Cheat Sheet I found, which can be used to quickly find related syntaxes.
Learn Regular Expressions
I have seen a fairly good quick Learning Guide for regular expressions on the Internet. If you are interested in further study, you can refer to it.
Online Testing Tool for Regular Expressions
Regex101 is a very good online testing tool for regular expressions. You can test your regular expressions online.
In addition, I also found some good Regular Expression tutorials and books on the Internet and shared them to jijiangshe.com. If you are interested in learning, please visit. Pai_^
Link to the original article: knowing these 20 regular expressions allows you to write less than 1000 lines of code