Regular expressions for regular expression instances

Source: Internet
Author: User
This article mainly introduces the regular expression of common examples of collation, very good, with reference value, the need for friends can refer to the next

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

1. Verifying 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. Check Password strength

The strength of the password must be a combination of uppercase and lowercase letters and numbers, no special characters, and a length of 8-10.

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

3. Verifying Chinese strings can only be in Chinese.

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

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

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

5. Verify the E-mail address is the same as the password, the following is the e-mail address compliance of the regular check statement.

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

6. Check the ID number

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

15-bit: var reg =/^[1-9]\\d{7} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) \\d{3}$/;18 bit: 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. Verify the date checksum in the date "YYYY-MM-DD" format, considering a 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 amount to 2 decimal places.

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

9. Check the phone number

The following is the domestic mobile phone number 13, 15, 18, the regular expression. (Can be expanded according to the current domestic collection number of the first two-digit opening 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. To determine IE version IE has not been completely replaced, many pages still need to do version compatibility, the following is the version of IE edition check expression.

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

11. Verifying 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. Verifying 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}:) {] (: [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

Many times in application development need to distinguish between the request is HTTPS or HTTP, the following expression can be taken out of a URL prefix and then logical judgment.

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

14. Extracting URL Links

The following expression can filter out URLs in a text.

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

15. File paths and extended schools

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

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

16. Extract the 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 Images If you want to extract all the image information from a webpage, you can use the following expression.

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

18. Extract page hyperlinks extracts hyperlinks in HTML.

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

19. Find CSS Properties

The following expression allows you to search for matching CSS properties.

var reg =/^\\s*[a-za-z\\-]+\\s*[:]{1}\\s[a-za-z0-9\\s.#]+[;] {1}/;

20. Extracting annotations

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

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

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.