SoFunction
Updated on 2025-03-03

Commonly used regular expression examples

Collect regular expression instances that are often used in business, so as to facilitate future searches and reduce workload.

1. Verify the basic date format

var  reg1 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;
var  reg2 = /^(^(\d{4}|\d{2})(\-|\/|\.)\d{1,2}\3\d{1,2}$)|(^\d{4}Year\d{1,2}moon\d{1,2}day$)$/;

2. Verify password strength

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

var  reg = /^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$/;

3. Verify that the Chinese string can only be Chinese.

var  reg = /^[\\u4e00-\\u9fa5]{0,}$/;

4. A string composed of numbers, 26 English letters or underscores

var reg = /^\\w+$/;

5. Verify the E-mail address. Like the password, the following is a regular check statement for the compliance of the E-mail address.

var  reg = 
/[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?/;

6. Verify ID number

Below is the regular verification of the ID number. 15 or 18 bits.

15Bit: var reg = /^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/;
18Bit:var  reg = /^[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)$/;

7. Check date The date check in the format of "yyyy-mm-dd" has taken into account the flat leap year.

var reg = 
/^(?:(?!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)$/;

8. Verify the amount and check the amount, accurately to 2 decimal places.

var  reg = /^[0-9]+(.[0-9]{2})?$/;

9. Verify mobile phone number

Below are the domestic regular expressions of mobile phone numbers starting from 13, 15, and 18. (The first two digits can be expanded based on the current domestic collection number)

var reg = /^(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}$/;

10. It is judging that the IE version of IE has not been completely replaced yet, and many pages still need to be version compatible. The following is the expression for IE version checking.

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

11. Verify IP-v4 address

var  reg = 
/\\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/;

12. Verify IP-v6 address

var reg = 
/(([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]))/
;

13. Check the prefix of the URL

In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. Through the following expression, you can take out the prefix of a url and then make logical judgments.

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

14. Extract URL link

The following expression can filter out URLs in a piece of text.

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

15. File path and extension verification

Verify the file path and extension under Windows (in the example below, the .txt file)

var reg = /^([a-zA-Z]\\:|\\\\)\\\\([^\\]+\\)*[^\\/:*?"<>|]+\\.txt(l)?$/;

16. Extract Color Hex Codes

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

var  reg = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;

17. Extract web page pictures. If you want to extract all image information on the web page, you can use the following expression.

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

18. Extract page hyperlink Extract hyperlinks in html.

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

19. Find CSS Properties

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

var reg = /^\\s*[a-zA-Z\\-]+\\s*[:]{1}\\s[a-zA-Z0-9\\s.#]+[;]{1}/;

20. Extract comments

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

var reg = /<!--(.*?)-->/;

Summarize