Knowing these 20 regular expressions can save 1000 lines of code

Posted by KFC on Mon, 03 Jan 2022 20:10:08 +0100

Regular expression, a very old and powerful text processing tool, can quickly realize a very complex business logic with only a very short expression statement. Mastering regular expressions can greatly improve your development efficiency.

Regular expressions are often used to verify fields or arbitrary strings, such as the following JavaScript code to verify 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!');           

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

1 . Verify password strength

The strength of the password must be a combination of upper and lower case letters and numbers. Special characters cannot be used, and the length is between 8-10.


2. Verify Chinese

String can only be Chinese.


3. A string consisting of numbers, 26 English letters or underscores


4. Verify E-Mail address

Like passwords, the following is a regular check statement for E-mail address compliance.


5. verify ID number.

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

15 bits:


18 bits:


6. Verification date

Date verification in "yyyy MM DD" format, taking into account flat leap years.


7. Verification amount

Amount verification, accurate to 2 decimal places.


8. Verify mobile phone number

The following is the regular expression of domestic mobile phone numbers beginning with 13, 15 and 18. (the first two digits can be extended according to the current domestic collection number)


9. Judge the IE version

IE has not been completely replaced yet. Many pages still need to be version compatible. 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.


11. Verify IP-v6 address

IP6 regular statement.


12. Check the prefix of the URL

In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. The prefix of a url can be taken out through the following expression and then judged logically.

if (!s.match(/^[a-zA-Z]+:\\/\\//))
    s = 'http://' + s;

13. Extract URL links

The following expression can filter out the URL 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 (the. txt file in the following example)


15. Extract Color Hex Codes

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


16. Extract web page pictures

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

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

17. Extract page hyperlinks

Extract hyperlinks in html.

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

18. Find CSS properties

Through the following expression, you can search for the matching CSS properties.


19. Extract notes

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


20. Match HTML tags

The tag attributes in HTML can be matched by the following expression.


Syntax of regular expressions

The following is a very good regular expression Cheat Sheet, which can be used to quickly find relevant syntax.

If you feel helpful, please tell me more and let's make progress together!

Those interested in Java learning and Internet technology discussion can add groups, waiting for you: 736953707 QQ group

Topics: Front-end Programmer Deep Learning regex computer