20 Regular Expressions That Can Save You 1000 Lines of Code

Regular expressions, a very old and powerful text processing tool, can quickly implement a very complex business logic with only a very short expression statement. If you master regular expressions proficiently, your development efficiency can be greatly improved.

Regular expressions are often used to validate fields or arbitrary strings, such as the following JavaScript code that validates basic date formats:
var reg = /^(\\d{1,4})(-|\\/)(\\d{1,2})\\2(\\d{1,2})$/;
var r = fieldValue.match (reg);             
if(r==null)alert('Date format error!');


The following are 20 regular expressions that are often used in front-end development.

1. Verify the strength of the
password of the password must be a combination of uppercase and lowercase letters and numbers, no special characters, and a length between 8 and 10.
^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$


2. Check Chinese The
string can only be in Chinese.
^[\\u4e00-\\u9fa5]{0,}$


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


4. Verifying the E-mail address
As with the password, the following is a regular check statement for the compliance of the E-mail address.
[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?


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

15 bits:
^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$


18th place:
^[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.
The date verification in the format "yyyy-mm-dd" of the verification date, taking into account the 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)$


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


8. Check the mobile phone number
The following is the regular expression of domestic mobile phone numbers starting with 13, 15, and 18. (The first two numbers can be expanded according to the current domestic collection 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}$


9. Judging the version of
IE IE has not yet been completely replaced, and many pages still need to be version compatible. The following is the expression for checking the IE version.
^.*MSIE [5-8](?:\\.[0-9]+)?(?!.*Trident\\/[5-9]\\.0).*$


10. Check 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


11. Verify 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. Through the following expression, a URL prefix can be extracted and then logically judged.
if (!s.match(/^[a-zA-Z]+:\\/\\//)){
    s = 'http://' + s;
}


13. Extract URL links
The following expression filters out URLs in a piece of text.
^(f|ht){1}(tp|tps):\\/\\/([\\w-]+\\.)+[\\w-]+(\\/[\\w- ./?%&=]*)?


14. File path and extension verification
Verify the file path and extension under Windows (.txt file in the example below)
^([a-zA-Z]\\:|\\\\)\\\\([^\\\\]+\\\\)*[^\\/:*?"<>|]+\\.txt(l)?$


15. Extract Color Hex Codes
Sometimes it is necessary to extract the color codes in web pages, you can use the following expressions.
^#([A-Fa-f0-9] {6} | [A-Fa-f0-9] {3}) $


16. Extract web images
If you want to extract all image information in web pages, you can use the following expressions.
\\< *[img][^\\\\>]*[src] *= *[\\"\\']{0,1}([^\\"\\'\\ >]*)


17. Extract page hyperlinks
Extract in html.
(<a\\s*(?!.*\\brel=)[^>]*)(href="https?:\\/\\/)((?!(?:(?:www\\.)?'.implode('|(?:www\\.)?', $follow_list).'))[^"]+)"((?!.*\\brel=)[^>]*)(?:[^>]*)>


18. Find CSS properties
Through the following expressions, you can search for matching CSS properties.
^ \\ s * [a-zA-Z \\ -] + \\ s * [:] {1} \\ s [a-zA-Z0-9 \\ s. #] + [;] {1}


19. Extract Comments
If you need to remove comments from HMTL, you can use the following expressions.
<!--(.*?)-->


20. Match HTML tags The tag attributes in HTML can be matched
by the following expressions.
<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>


Regular Expression Relevant Syntax

Below is a nice regular expression cheat sheet I found that can be used to quickly find relevant syntax.


Regular expression online testing tool

regex101 is a very good regular expression online testing tool, you can directly test your regular expressions online.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326579153&siteId=291194637