Summary of the most complete regular expressions on the whole web to make your work more efficient and efficient

Posted by jediman on Tue, 22 Feb 2022 18:29:43 +0100

Hello, I'm Glacier~~

I spent two days sorting out these tasks with regular expressions that I often use. My little friends take them away. Thank you.

This time I shared the regular expressions that I often used in my work. It is precisely these regular expressions that I have mastered. Ice River writes an average of 200 lines less code per day than others, which greatly improves the efficiency of research and development. I recommend that small partners collect them and try to use them in their own projects in the ordinary time!!

A good command of regular expressions can help programmers write the most elegant code at the fastest speed.

Ice River has been programming for many years. It has combed and summarized the regular expressions that have been used. These regular expressions can help you save a lot of coding time. Often a simple regular expression can omit a lot of if...else... Code.

This time, Ice River has opened its regular expressions to its little partners, which it often uses, in the hope that they can get substantial help.

Ice River Common Regular

Integer or decimal


Only numbers can be entered


You can only enter n-bit numbers


You can only enter numbers with at least n digits


You can only enter m~n digits


You can only enter numbers that start with zero or non-zero


Only positive real numbers with two decimals can be entered


Only positive real numbers with 1-3 decimal places can be entered


Only non-zero positive integers can be entered


Only non-zero negative integers can be entered


Only characters of length 3 can be entered


Only strings of 26 letters can be entered


Only strings of 26 uppercase letters can be entered


Only strings of 26 lowercase letters can be entered


Only strings consisting of numbers and 26 letters can be entered


Only strings consisting of numbers, 26 English letters, or underscores can be entered


Verify user password:


Note: The correct format is: start with a letter, 6-18 in length, and can only contain characters, numbers and underscores.

Verify that ^%&,;=?$ Equal Character


Only Chinese characters can be entered


Verify Email Address


Verify Internet URL


Verify phone number



Authentication ID number (15 or 18 digits)


Validate 12 months of a year


The correct formats are: 01-09 and 1-12

Verify 31 days of a month


The correct format is; 01-09 and 1-31

Regular expressions matching Chinese characters


Match double-byte characters (including Chinese characters)


Regular expression matching empty lines

n[s| ]*r

Regular expressions matching html tags


Regular expression matching first and last spaces


A regular expression matching an Email address


Regular expressions matching HTML Tags

<(S*?)[^>]*>.*?|<.*? />

Note: The version that is streaming online is too bad, and this one matches only part of it, and there is nothing you can do about complex nested tags.

Regular expression matching white space at beginning and end


Comment: Useful expressions that can be used to delete blank characters (including spaces, tabs, page breaks, etc.) at the beginning and end of a line

A regular expression matching an Email address


Comment: Useful for form validation

Regular expression matching URL of web address


Note: Versions that are streamed online have limited functionality, and this is basically what you need.

Is the matching account number legal (letters start, allow 5-16 bytes, allow alphanumeric underscores)


Comment: Useful for form validation

Match domestic phone number


Comment: Match form such as 0511-4405222 or 021-87888822

Match Tencent QQ


Comment: Tencent QQ starts from 10000

Match China Postal Code


Comment: China Postal Code is 6 digits

Match ID


Note: China has 15 or 18 ID cards

Match ip address


Note: Useful for extracting ip addresses

Match a specific number

^[1-9]d*$ //Match positive integer
^-[1-9]d*$ //Match Negative Integer
^-?[1-9]d*$ //Match Integer
^[1-9]d*|0$ //Match nonnegative integers (positive integer + 0)
^-[1-9]d*|0$ //Match nonpositive integers (negative integer + 0)
^[1-9]d*.d*|0.d*[1-9]d*$ //Match positive floating point numbers
^-([1-9]d*.d*|0.d*[1-9]d*)$ //Match negative floating point numbers
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ //Matching floating point numbers
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$ //Match nonnegative floating point numbers (positive + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$//Matching non-positive floating point numbers (negative floating point + 0)s

Note: Useful when dealing with large amounts of data, pay attention to corrections when applying.

Match a specific string

^[A-Za-z]+$//Matches a string of 26 letters
^[A-Z]+$//Matches a string of 26 uppercase letters
^[a-z]+$//Matches a string of 26 lower case letters
^[A-Za-z0-9]+$//Match strings consisting of numbers and 26 English letters
^w+$//Matches a string of numbers, 26 English letters, or underscores

Note: Some of the most basic and common expressions

Check password strength such as password strength: contains a combination of upper and lower case letters and numbers, cannot use special characters, length is between 8-10.


Check String



A string of numbers, 26 English letters, or underscores


Verify E-Mail address


Verify ID number 15 digits:


18 bits:


Date validation in the format yyyy-mm-dd, with leap year considered.


Validate amount to 2 decimal places.


Below the check mobile number are regular expressions for mobile numbers starting at 13, 15 and 18 in China. (The first two digits starting number can be expanded according to the current domestic collection number)


Determine the version of IE

^.*MSIE [5-8](?:.[0-9]+)?(?!.*Trident/[5-9].0).*$

Verify IP-v4 address


Verify IP-v6 Address


Check prefixes of URL s

In application development, it is often necessary to distinguish whether a request is HTTPS or HTTP. The following expression allows you to prefix a url and make logical decisions.

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

Extract URL Links

This expression below filters out URL s in a piece of text.

^(f|ht){1}(tp|tps)://([w-]+.)+[w-]+(/[w- ./?%&=]*)?

File Path and Extension Checks validate file paths and extensions under windows (in the following example, a.txt file)


Extracting the color code of a Web page sometimes requires extracting the color code of a Web page. You can use the following expression.


Extract pictures from web pages

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

Extract Page Hyperlinks

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

Find CSS Properties


Extract Comments


Match HTML Tags


Time Regular Case

Simple date judgment (YYYY/MM/DD)


Date Judgment of Evolution (YYYY/MM/DD| YY/MM/DD)

^(^(d{4}|d{2})(-|/|.)d{1,2}3d{1,2}$)|(^d{4}year d{1,2}month d{1,2}day $)$ 

Judgment of leap year addition




What is the legal date range? This question has different interpretations for different scenarios. This follows the conventions in MSDN:

The DateTime value type represents the date and time between 12:00:00 p.m. and 11:59:59 p.m. on January 1, 2001 and A.D. (C.E.) on December 31, 1999

Interpretation of leap years.

As for leap years in the Gregorian calendar, it is stipulated that one cycle of the earth's rotation around the sun is called a regression year, with a regression age of 365 days, 5:48 minutes and 46 seconds. Therefore, the Gregorian calendar stipulates that there are ordinary years and leap years. The average year has 365 days, which is 0.2422 days shorter than the year of return and 0.9688 days shorter than the year of return. Therefore, it increases by one day every four years. This year has 366 days, which is the leap year. However, four years with one day more than four regression years will be 0.0312 days more, and after 400 years there will be 3.12 days more. Therefore, there will be three less leap years in 400 years, that is, 97 leap years in 400 years, so the average length of Gregorian calendar years will be similar to that of regression years. It is thus stipulated that a leap year is only a multiple of 400 if the year is an integer hundred, for example 1900 and 2100 are not leap years.

First you need to verify the year. Obviously, the year range is 0001 - 9999, and the regular expression matching YYYY is:


Where [0-9] can also be expressed as d, but D is not as intuitive as [0-9], so I'll keep using [0-9]

There are two difficulties in validating dates with regular expressions: first, the number of days in different months, and second, leap year considerations.

For the first difficulty, let's not consider leap years first. Assuming that February is 28 days, month and date can be divided into three cases:

(1) Month is 1, 3, 5, 7, 8, 10, 12, day range 01 - 31, regular expression matching MM-DD is:


(2) Months are 4, 6, 9, 11, days range from 01-30, and the regular expression matching MM-DD is:


(3) Month is 2, and the regular expression matching MM-DD is:


Based on the above results, we can get regular expressions that match the flat year date format of YYYY-MM-DD:


Next, we address the second difficulty: leap year considerations. According to the definition of leap years, leap years can be divided into two categories:

(1) Year that can be divided by 4 but not by 100. Looking for the latter two changes, you can quickly get the following regular matches:


(2) The year divisible by 400. The number that can be divided by 400 must be divided by 100, so the last two must be 00. We just need to make sure that the first two can be divided by 4. The corresponding regular expression is:


Regular expression for the strongest validation date, with leap year validation added

The date format supported by this date regular expression is shown below.


The complete regular expression is as follows


February has 29 days for leap year, so the regular expression matching leap year dates in YYYY-MM-DD format is:


Finally, by combining the date validation expressions for flat and leap years, we find that the final validation date format is YYYY-MM-DD:


The regular validation expression for DD/MM/YYYY* format is:


Partners can collect first and then check out these common regular expressions!

Okay, let's get here today. I'm Ice River. You can leave a message if you have any questions, or you can trust me on WeChat. I will reply to you when I see it. Finally, the little buddies say yes, look at it, leave a message, forward it, and get up ~~

Write at the end

If you want to enter a big factory, want to get a promotion and pay raise, or are confused about your existing job, you can trust me to communicate with you. I hope some of my experiences can help you ~~

Recommended reading:

Okay, let's get here today. My little buddies like praises, collections, comments and one click at a time. I'm glacier. I'll see you next time~~

??? Search for the public number below to follow me???

Topics: Front-end html Back-end Interview regex