Sort Regular Expression instances and regular expression instances

Source: Internet
Author: User

Sort Regular Expression instances and regular expression instances

Collect Regular Expression instances that are frequently used in the business to facilitate future search and reduce the 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} month \ d {1, 2} day $) $ /;

2. Verify password strength

The password must be a combination of uppercase and lowercase letters and numbers. special characters are not allowed and the password length must be between 8 and 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 consisting of digits, 26 English letters, or underscores

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

5. Check the e-mail address is the same as 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 the ID card number

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

15-bit: var reg =/^ [1-9] \ d {7} (0 \ d) | (1 [0-2]) ([0 | 1 | 2] \ d) | 3 [0-1]) \ d {3} $/; 18 bits: 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. Date verification in the format of "yyyy-mm-dd" has been taken into consideration.

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, accurate to 2 decimal places.

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

9. Verify the mobile phone number

The following is a regular expression for mobile phone numbers starting with "13", "15", and "18" in China. (According to the current domestic collection number, expand the first two numbers)

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. Check whether the IE version has not been completely replaced. Many pages still require version compatibility. The following is the expression for IE version check.

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

11. Verify the 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 the 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 URL prefix.

In application development, it is often necessary to distinguish whether the request is HTTPS or HTTP. The following expression can be used to retrieve the prefix of a url and then perform logic judgment.

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

14. Extract URL links

The following expression filters out the 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 names and extensions under Windows (The following example is the. txt file)

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

16. extract Color Hex Codes

You can use the following expression to extract the color code from a webpage.

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

17. Extract webpage images. If you want to extract all the image information on the webpage, you can use the following expression.

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

18. Extract page hyperlinks extract html hyperlinks.

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

19. Search for CSS attributes

The following expression can be used to search for matching CSS attributes.

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

20. Extract comments

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

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

Summary

The above is a list of common Regular Expression examples introduced by Alibaba Cloud. I hope this will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.