JavaScript Common Regular Example

Source: Internet
Author: User

1. Trim function (clear the string at both ends of the space)

String.prototype.trim = function () {
Return This.replace (/(^\s+) | ( \s+$)/g, ");

};

' abc '. Trim ();
Output "ABC"

2. Extracting parameter names and values for URLs in the browser

function Geturlparam (URL) {

var result = {},

url = URL | | Window.location.href,

reg =/([\?| &]) (. +?) = ([^&?] *)/gi,

arr = reg.exec (URL);

while (arr) {
RESULT[ARR[2]] = arr[3];

arr = reg.exec (URL);

}

return result;

}

Geturlparam (' https://www.baidu.com?a=0&b=1&c=2 ');
Output "{A:" 0 ", B:" 1 ", C:" 2 "}"

3. Expand the typeof to ascertain the specific type

function GetType (obj) {
var result = Object.prototype.toString.call (obj);
result = Result.replace (/\[object\s (\w+) \]/, ' $ ');

return result;

}

GetType ([up]);
Output "Array"
GetType (NULL);
Output "Null"

4. Inserting new characters at a specified position in a string

String.prototype.insertAt = function (str, index) {

Index + = 1;

var reg = new RegExp (' (^.{' + Index + '}) ');

Return This.replace (Reg, ' $ ' + str);

};

' ABCDE '. InsertAt (' 123 ', 2);
Output "Abc123de"

5. Hide the middle 4 digits of the phone number

function Telformat (tel) {

Tel = String (tel);

Return Tel.replace (/(\d{3}) (\d{4}) (\d{4})/, ' $1****$3 ');

}

Telformat (13899207998);
Output "138****7998"

6. String Exchange

var name = "Doe Jone";

Name.replace (/(\w+) \s* \s* (\w+)/, "$ $");
Output "Jone Doe"

7. String interception

var str = ' ASFDF = = = = Sdfaf # ';

Str.match (/[^===]+ (=[===))/g);

Output "ASFDF"

8. Check Password strength regular

Must be a combination of uppercase and lowercase letters and numbers, cannot use special characters, and is between 8-10 in length.

/^ (? =.*\d) (? =.*[a-z]) (? =.*[a-z]). {8,10}$/.test ("weeeeeeeW2");
Output true

Password strength Regular, minimum 6 bits, including at least 1 uppercase letters, 1 lowercase letters, 1 digits, 1 special characters

/^.*(?=. {6,}) (? =.*\d) (? =.*[a-z]) (? =.*[a-z]) (? =.*[[email protected]#$%^&*?]). *$/.test ("diaod123#");

Output true

9. Detecting Chinese characters

Detects if a string contains only Chinese

/^[\u4e00-\u9fa5]{0,}$/.test ("but D");
Output false

/^[\u4e00-\u9fa5]{0,}$/.test ("but");
Output true

Includes a Chinese

/[\u4e00-\u9fa5]/.test ("but D");
Output true

10. Regular ID Card number

/^[1-9]\d{5} (18|19| ( [23]\d)] \d{2} ((0[1-9]) | ( 10|11|12)) (([0-2][1-9]) |10|20|30|31) \d{3}[0-9xx]$/.test ("42112319870115371X");

Output false

11. Check the date ("YYYY-MM-DD" format, consider the flat leap year)

Date regular, simple judgment, no month and date judgment

var dP1 =/^\d{4} (\-) \d{1,2}\1\d{1,2}$/;

Console.log (Dp1.test ("2017-05-11"));

Output true

Console.log (Dp1.test ("2017-15-11"));
Output true

Date regularization, complex judgment

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

Console.log (Dp2.test ("2017-02-11"));
Output true

Console.log (Dp2.test ("2017-15-11"));

Output false

Console.log (Dp2.test ("2017-02-29"));

Output false

12. Floating-point number regularization

Verify that it is a floating-point number

/^(?:[-+])? (?: [0-9]+)? (?:\. [0-9]*]? (?: [ee][\+\-]? (?: [0-9]+)]? $/.test (0.2);
Output true

E- Mail regular

Verify Email
/^ ([a-za-z0-9_\-\.]) +\@ ([a-za-z0-9_\-\.]) +\. ([a-za-z]{2,4}) $/.test ("[email protected]");

1. Mailboxes start with A-Z, A-Z, 0-9 and a minimum length of 1.

2. If the left part contains-, _,. These special symbols must be preceded by a number or letter.

[Email protected] symbol is required

4. The right part can be divided into two parts, the first part of the mail provider domain name address, the second part of the domain name suffix, is now known as the shortest 2 bits.

The longest is 6 for.

5. The mail provider domain can contain special characters-, _,.

/^[a-z0-9]+ ([. _\\-]*[a-z0-9]) *@ ([a-z0-9]+[-a-z0-9]*[a-z0-9]+.) {1,63} [A-z0-9]+$/.test ("[email protected]");
Output true

14. Verifying the fax number

Country code (2 to 3 digits)-area code (2 to 3 digits)-Phone number (7 to 8 digits)-Extension (3-bit)

/^ ([0\+]\d{2,3}-)? (0\d{2,3})-) (\d{7,8}) (-(\d{3,}))? $/.test (' 021-5055455 ');
Output true

15. Mobile phone Number Regular

/^1[34578]\d{9}$/.test ("13611778887");

Output true

* 13 Segment: 130, 131, 132, 133, 134, 135, 136, 137, 138, 139

* 14 segment: 145, 147

* 15 Segment: 150, 151, 152, 153, 155, 156, 157, 158, 159

* 17 Segment: 170, 176, 177, 178

* 18 Segment: 180, 181, 182, 183, 184, 185, 186, 187, 188, 189

* International codes such as: China (+86)

/^((\+? [0-9] {1,4}) | (\ (\+86\)))? (13[0-9]|14[57]|15[012356789]|17[03678]|18[0-9]) \d{8}$/.test ("1

Output true

URL Regular

/^ ((https?| Ftp|file): \/\/)? ([\da-z\.-]+) \. ([A-z\.] {2,6}) ([\/\w \.-]*) *\/?$/.test ("http://wangchujiang.com");

Output true

Get the URL in the domain name, the protocol regular ' http://xxx.xx/xxx ', ' https://xxx.xx/xxx ', '//xxx.xx/xxx '

/^ (http (?: | s) \:) *\/\/([^\/]+)/.test ("http://www.baidu.com");

Output true

/^ ((HTTP|HTTPS): \/\/(\w+:{0,1}\w*@)? (\s+) |) (: [0-9]+)? (\/|\/([\w#!:.? +=&%@!\-\/])? $/.test (' Https://www.baidu.com/[email protected]#%$^&%$# ');

Output true

There must be an agreement.

/^[a-za-z]+:\/\//.test ("http://www.baidu.com");
Output true

RGB Hex Color Regular

/^#? ([a-fa-f0-9]{6}| [A-fa-f0-9] {3}) $/.test ("#b8b8b8");

Output true

18. The license plate number is regular

/^[Jing Jin Shanghai yu ji yu Yun liao Black Xiang Wan Lu xin su zhe jiang Gan Guiganjin Mongolia Shaanxi Gi Jingui Guangdong Qinghai-Tibet Kanningjong led a-z]{1}[a-z]{1}[a-z0-9]{4}[a-z0-9 Hung learning Police Hong Kong and Macao]{1}$/.test ("Shanghai B99116");
Output true

19. Check Number

6 to 20 digits, beginning with letter, letter, number, minus sign, underline
/^[a-za-z] ([-_a-za-z0-9]{5,19}) +$/.test ("Jslite");
Output true

20. Check QQ number

5 to 11 bits

/^[1-9][0-9]{4,10}$/.test ("398188661");
Output true

JavaScript Common Regular Example

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.