JavaScript regular expression syntax and Examples

Source: Internet
Author: User

JavaScript regular expression syntax and Examples

JavaScript Regular Expression

1. To use a JS regular expression, you must first understand the common symbols of the JS regular expression, such:

/... /

Indicates the start and end of a mode.

^

Start of matching string

$

End of matching string

\ S

Match a blank character

\ S

Match a non-blank character

\ D

Matches a number, which is equivalent to [0-9].

\ D

Match a non-numeric character, equivalent to [^ 0-9]

\ W

Match a number, letter, or underline, equivalent to [A-Za-z0-9 _]

.

Match a character other than a line break

\

Escape characters. If you want to match these special symbols, you must add \ for escape.

{N}

Match the previous item N times

{N ,}

Match the previous N to multiple times

{N, m}

Match the first item N to M times, that is, match at least N times and match at most M times

*

Match the first item 0 to multiple times, equivalent to {0 ,}

+

Match the first item one to multiple times, equivalent to {1 ,}

?

Match the first item 0 to 1, equivalent to {0, 1}

2. Use a regular expression

To use a regular expression, you must use the RegExp object (RegularExpression) to verify the input

Whether the content meets the rules.

Create a regular expression in either of the following ways:

1. Common method: var reg =/expression/additional parameter;

2. constructor: var reg = new RegExp (expression, additional parameter );

Additional parameters can be understood as a pattern, that is, when expressions are used to limit, some general filtering modes are added. For example

G

Global match

I

Case Insensitive

M

Multi-row matching

Rules for adding parameters:

1. parameters can be combined or not added.

2. expressions can also be used to directly write strings.

3. In normal mode, the expression must be a constant, but a variable can be used in the expression in the constructor.

Common Regular Expressions:

1. How to return true if the RegExp object's test (string to be tested) method complies with the rules; otherwise, false is returned;

2. search (reg) method of the String object to check whether the String contains a String that complies with the rules.

3. replace (reg) method of the String object to replace the regular strings in the String.

4. split (reg) method of the String object. The source String is divided into arrays according to the matching strings.

 

Additional usage example:

 

/*

Purpose: Check whether the entered Email address format is correct.

Input:

StrEmail: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkEmail (strEmail ){

// Var emailReg =/^ [_ a-z0-9] + @ ([_ a-z0-9] + \.) + [a-z0-9] {2, 3} $ /;

Var emailReg =/^ [\ w-] + (\. [\ w-] +) * @ [\ w-] + (\. [\ w-] +) + $ /;

If (emailReg. test (strEmail )){

Return true;

} Else {

Alert ("the Email address format you entered is incorrect! ");

Return false;

}

}

 

/*

Purpose: Verify the IP address format.

Input: strIP: IP Address

Return: If the verification succeeds, true is returned; otherwise, false is returned;

*/

Function isIP (strIP ){

If (isNull (strIP) return false;

Var re =/^ (\ d + )\. (\ d + )\. (\ d + )\. (\ d +) $/g // Regular Expression matching IP addresses

If (re. test (strIP ))

{

If (RegExp. $1 <256 & RegExp. $2 <256 & RegExp. $3 <256 & RegExp. $4 <256) return true;

}

Return false;

}

 

/*

Purpose: Check whether the entered mobile phone number is correct.

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkMobile (s ){

Var regu =/^ [1] [3] [0-9] {9} $ /;

Var re = new RegExp (regu );

If (re. test (s )){

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: Check whether the entered phone number format is correct.

Input:

StrPhone: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkPhone (strPhone ){

Var phoneRegWithArea =/^ [0] [1-9] {2, 3}-[0-9] {5, 10} $ /;

Var phoneRegNoArea =/^ [1-9] {1} [0-9] {5, 8} $ /;

Var prompt = "the phone number you entered is incorrect! "

If (strPhone. length> 9 ){

If (phoneRegWithArea. test (strPhone )){

Return true;

} Else {

Alert (prompt );

Return false;

}

} Else {

If (phoneRegNoArea. test (strPhone )){

Return true;

} Else {

Alert (prompt );

Return false;

}

 

}

}

 

/*

Purpose: Check whether the input string is null or all are spaces.

Input: str

Return Value:

If all values are null, true is returned. Otherwise, false is returned.

*/

Function isNull (str ){

If (str = "") return true;

Var regu = "^ [] + $ ";

Var re = new RegExp (regu );

Return re. test (str );

}

 

/*

Purpose: Check whether the value of the input object conforms to the integer format.

Input: str string

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isInteger (str ){

Var regu =/^ [-] {0, 1} [0-9] {1,} $ /;

Return regu. test (str );

}

 

/*

Purpose: Check whether the input string conforms to the positive integer format.

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isNumber (s ){

Var regu = "^ [0-9] + $ ";

Var re = new RegExp (regu );

If (s. search (re )! =-1 ){

Return true;

} Else {

Return false;

}

}

/*

Purpose: Check whether the input string is in decimal number format. It can be a negative number.

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isDecimal (str ){

If (isInteger (str) return true;

Var re =/^ [-] {0, 1} (\ d +) [\.] + (\ d +) $ /;

If (re. test (str )){

If (RegExp. $1 = 0 & RegExp. $2 = 0) return false;

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: Check whether the value of the input object conforms to the port number format.

Input: str string

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isPort (str ){

Return (isNumber (str) & str <65536 );

}

 

/*

Purpose: Check whether the input string meets the amount format

The format is defined as a positive number with decimal places. A maximum of three decimal places can be entered.

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isMoney (s ){

Var regu = "^ [0-9] + [\.] [0-9] {0, 3} $ ";

Var re = new RegExp (regu );

If (re. test (s )){

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: Check whether the input string is composed of English letters, numbers, and underscores (_).

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isNumberOr_Letter (s) {// determines whether it is a number or letter

Var regu = "^ [0-9a-zA-Z \ _] + $ ";

Var re = new RegExp (regu );

If (re. test (s )){

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: Check whether the input string consists of only English letters and numbers.

Input:

S: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isNumberOrLetter (s) {// determines whether it is a number or letter

Var regu = "^ [0-9a-zA-Z] + $ ";

Var re = new RegExp (regu );

If (re. test (s )){

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: Check whether the input string consists of only Chinese characters, letters, and numbers.

Input:

Value: String

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function isChinaOrNumbOrLett (s) {// determines whether it is composed of Chinese characters, letters, and numbers.

Var regu = "^ [0-9a-zA-Z \ u4e00-\ u9fa5] + $ ";

Var re = new RegExp (regu );

If (re. test (s )){

Return true;

} Else {

Return false;

}

}

 

/*

Purpose: determine whether it is a date.

Input: date; fmt: date Format

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isDate (date, fmt ){

If (fmt = null) fmt = "yyyyMMdd ";

Var yIndex = fmt. indexOf ("yyyy ");

If (yIndex =-1) return false;

Var year = date. substring (yIndex, yIndex + 4 );

Var mIndex = fmt. indexOf ("MM ");

If (mIndex =-1) return false;

Var month = date. substring (mIndex, mIndex + 2 );

Var dIndex = fmt. indexOf ("dd ");

If (dIndex =-1) return false;

Var day = date. substring (dIndex, dIndex + 2 );

If (! IsNumber (year) | year> "2100" | year <"1900") return false;

If (! IsNumber (month) | month> "12" | month <"01") return false;

If (day> getMaxDay (year, month) | day <"01") return false;

Return true;

}

Function getMaxDay (year, month ){

If (month = 4 | month = 6 | month = 9 | month = 11)

Return "30 ";

If (month = 2)

If (year % 4 = 0 & amp; year % 100! = 0 | year % 400 = 0)

Return "29 ";

Else

Return "28 ";

Return "31 ";

}

 

/*

Purpose: whether character 1 ends with string 2

Input: str1: string; str2: contained string

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isLastMatch (str1, str2)

{

Var index = str1.lastIndexOf (str2 );

If (str1.length = index + str2.length) returntrue;

Return false;

}

 

/*

Purpose: whether character 1 starts with string 2

Input: str1: string; str2: contained string

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isFirstMatch (str1, str2)

{

Var index = str1.indexOf (str2 );

If (index = 0) return true;

Return false;

}

 

/*

Purpose: character 1 is a string containing 2

Input: str1: string; str2: contained string

Return: If the verification succeeds, true is returned; otherwise, false is returned.

*/

Function isMatch (str1, str2)

{

Var index = str1.indexOf (str2 );

If (index =-1) return false;

Return true;

}

 

/*

Purpose: Check whether the input start and end dates are correct. The rules are in the correct format for the two dates,

End on schedule> = Start Date

Input:

StartDate: start date, string

EndDate: end date, string

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkTwoDate (startDate, endDate ){

If (! IsDate (startDate )){

Alert ("Incorrect start date! ");

Return false;

} Else if (! IsDate (endDate )){

Alert ("the end date is incorrect! ");

Return false;

} Else if (startDate> endDate ){

Alert ("the start date cannot be later than the end date! ");

Return false;

}

Return true;

}

 

/*

Purpose: Check the number of selected check boxes.

Input:

CheckboxID: String

Return Value:

Returns the number of selected items in the check box.

*/

Function checkSelect (checkboxID ){

Var check = 0;

Var I = 0;

If (document. all (checkboxID). length> 0 ){

For (I = 0; I <document. all (checkboxID). length; I ++ ){

If (document. all (checkboxID). item (I). checked ){

Check + = 1;

}

 

 

}

} Else {

If (document. all (checkboxID). checked)

Check = 1;

}

Return check;

}

Function getTotalBytes (varField ){

If (varField = null)

Return-1;

Var totalCount = 0;

For (I = 0; I <varField. value. length; I ++ ){

If (varField. value. charCodeAt (I) & gt; 127)

TotalCount + = 2;

Else

TotalCount ++;

}

Return totalCount;

}

Function getFirstSelectedValue (checkboxID ){

Var value = null;

Var I = 0;

If (document. all (checkboxID). length> 0 ){

For (I = 0; I <document. all (checkboxID). length; I ++ ){

If (document. all (checkboxID). item (I). checked ){

Value = document. all (checkboxID). item (I). value;

Break;

}

}

} Else {

If (document. all (checkboxID). checked)

Value = document. all (checkboxID). value;

}

Return value;

}

 

Function getFirstSelectedIndex (checkboxID ){

Var value =-2;

Var I = 0;

If (document. all (checkboxID). length> 0 ){

For (I = 0; I <document. all (checkboxID). length; I ++ ){

If (document. all (checkboxID). item (I). checked ){

Value = I;

Break;

}

}

} Else {

If (document. all (checkboxID). checked)

Value =-1;

}

Return value;

}

Function selectAll (checkboxID, status ){

If (document. all (checkboxID) = null)

Return;

If (document. all (checkboxID). length> 0 ){

For (I = 0; I <document. all (checkboxID). length; I ++ ){

Document. all (checkboxID). item (I). checked = status;

}

} Else {

Document. all (checkboxID). checked = status;

}

}

Function selectInverse (checkboxID ){

If (document. all (checkboxID) = null)

Return;

If (document. all (checkboxID). length> 0 ){

For (I = 0; I <document. all (checkboxID). length; I ++ ){

Document. all (checkboxID). item (I). checked =! Document. all (checkboxID). item (I). checked;

}

} Else {

Document. all (checkboxID). checked =! Document. all (checkboxID). checked;

}

}

Function checkDate (value ){

If (value = '') return true;

If (value. length! = 8 |! IsNumber (value) return false;

Var year = value. substring (0, 4 );

If (year> "2100" | year <"1900 ")

Return false;

Var month = value. substring (4, 6 );

If (month> "12" | month <"01") return false;

Var day = value. substring (6, 8 );

If (day> getMaxDay (year, month) | day <"01") return false;

Return true;

}

/*

Purpose: Check whether the input start and end dates are correct. The rules are correct or empty.

And end date> = Start Date

Input:

StartDate: start date, string

EndDate: end date, string

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkPeriod (startDate, endDate ){

If (! CheckDate (startDate )){

Alert ("Incorrect start date! ");

Return false;

} Else if (! CheckDate (endDate )){

Alert ("the end date is incorrect! ");

Return false;

} Else if (startDate> endDate ){

Alert ("the start date cannot be later than the end date! ");

Return false;

}

Return true;

}

 

/*

Purpose: Check whether the securities code is correct

Input:

SecCode: securities code

Return Value:

If the verification succeeds, true is returned. Otherwise, false is returned.

*/

Function checkSecCode (secCode ){

If (secCode. length! = 6 ){

Alert ("the length of the securities code should be 6 characters ");

Return false;

}

If (! IsNumber (secCode )){

Alert ("securities code can only contain numbers ");

 

Return false;

}

Return true;

}

 

/*

Function: cTrim (sInputString, iType)

Description: String space-removing Function

Parameters: iType: 1 = remove the space on the left of the string

2 = remove the space on the left of the string

0 = remove spaces on the left and right of the string

Return value: a string that removes spaces.

*/

Function cTrim (sInputString, iType)

{

Var sTmpStr = '';

Var I =-1;

If (iType = 0 | iType = 1)

{

While (sTmpStr = '')

{

++ I;

STmpStr = sInputString. substr (I, 1 );

}

SInputString = sInputString. substring (I );

}

If (iType = 0 | iType = 2)

{

STmpStr = '';

I = sInputString. length;

While (sTmpStr = '')

{

-- I;

STmpStr = sInputString. substr (I, 1 );

}

SInputString = sInputString. substring (0, I + 1 );

}

Return sInputString;

}


Is there an error in the syntax example of the regular expression self-contained in javascript chm "matching a blank line?

Most of Baidu's websites are/^ \ [\ t] * $/Match a blank line.
So I went to google. (Because an article in China has been reposted N times, many of them have been reposted, and the wrong one has been taken as the correct one)

/^ \ S [\ t] * $/Match a blank line.
Note that \ and [have an s in the middle. Apparently \ s indicates Matches any white space character including space, tab, form-feed, and so on. Equivalent to [\ f \ n \ r \ t \ v].

What does $ in JavaScript regular expression syntax mean?

Javascript Regular Expressions/g and/I and/gi meanings regularexpression =/pattern/[switch]
This switch has three values.
G: Global match
I: case insensitive
Gi: The above combination // is like "". The "_" in the middle is the regular expression pattern, and the "g" in the name is the option. The method for declaring the matching is actually changing "_" to "$, personal opinions for your reference.

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.