Regular expression instance

  javascript, Regular expression

Collect regular expression instances that are often used in business to facilitate later search and reduce workload.

  1. Verify Basic Date Format
  2. Verify password strength
  3. Check Chinese
  4. A string of numbers, 26 English letters, or underscores
  5. Verify E-Mail address
  6. Verify ID Number
  7. Verification date
  8. Check amount
  9. Check the phone number
  10. Judging IE version
  11. Verify IP-v4 address
  12. Verify IP-v6 address
  13. Check prefix of URL
  14. Extract URL link
  15. File Path and Extension Verification
  16. Extract colorehecodes
  17. Extract web page pictures
  18. Select page hyperlink
  19. Find CSS properties
  20. Extract comments
  21. Convert thousands separator

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}年\d{1,2}月\d{1,2}日$)$/;

2. 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 and 10.

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

The solution provided by netizens to detect special characters

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

3. Verify Chinese

The string can only be Chinese.

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

4. String consisting of numbers, 26 English letters or underscores

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

5. verify E-Mail address

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

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

6. Verify ID number

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

15 digits:

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

18 digits:

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. Verification Date

The date check of “yyy-mm-dd” format has been considered for leap years.

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. Verification Amount

Amount verification, accurate to 2 decimal places.

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

9. Check the phone number

The following are the regular expressions for mobile phone numbers starting with 13, 15 and 18 in China. (The first two digits can be expanded 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}$/;

10. Judge the version of IE

IE has not been completely replaced at present. Many pages still need version compatibility. 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. The following expression can be used to take out the prefix of a url and then make a logical judgment.

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

14. extract URL link

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

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

16. extract colorehecodes

Sometimes it is necessary to extract color codes from web pages, and the following expression can be used.

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

17. Extract web page pictures

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

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

18. Select the page hyperlink

Select hyperlinks in html.

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

19. Find CSS properties

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

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

20. Extract comments

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

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

21. Convert thousands separator

return total.toString().replace(/\B(?=(\d{3})+$)/g, ',');