20 regular expressions!

Verification 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!');


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. The length is between 8-10.

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

2. Verify Chinese

String can only be Chinese.

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

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

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

4. Verify E-Mail address

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

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

5. Verify ID number

The following is the regular verification of ID number. 15 or 18 digits.

15 place:

var reg  =  /^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/;

18 place:

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)$/;

6. Verification date

Date verification in "yyyy MM DD" format, horizontal leap year has been considered.

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)$/;

7. Verification amount

Amount verification, accurate to 2 decimal places.

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

8. Verify mobile phone number

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

9. Judge IE version

At present, ie has not been completely replaced. Many pages still need to be version compatible. The following is the expression of IE version check.

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

10. Verify IP-v4 address

IP4 regular statement.

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/;

11. Verify IP-v6 address

IP6 regular statement.

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]))/;

12. Check the prefix of the URL

In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. You can get a url prefix through the following expression and then make a logical judgment.

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

13. Extract URL link

The following expression filters out the URL s in a piece of text.

var reg  =  /^(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)

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

15. Extract Color Hex Codes

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

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

16. Extract web page pictures

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

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

17. Extract page hyperlink

Extract hyperlinks from html.

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

18. Find CSS properties

With 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}/;

19. Extract notes

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

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

20. Matching HTML tags

You can match the tag attributes in HTML with the following expression.

var reg  =  /<\\/?\\w+((\\s+\\w+(\\s*=\\s*(?:".*?"|'.*?'|[\\^'">\\s]+))?)+\\s*|\\s*)\\/?>/;

Tags: Javascript IE Mobile Windows

Posted on Tue, 03 Dec 2019 06:40:36 -0500 by _DarkLink_