JS Regular Expression

Source: Internet
Author: User

This article from: http://www.cnblogs.com/wucg/archive/2010/04/09/1708440.html

Exec Method

Run the search in the string in regular expression mode and return an array containing the search result.

Rgexp.Exec (Str)

Parameters

Rgexp

Required. Which contains the regular expression mode and available flagRegular ExpressionObject.

Str

Required. TheStringObject or string text.

Description

IfExecIf the method does not find a match, it returnsNull. If it finds a matchExecMethod returns an array and updates the globalRegexpTo reflect the matching results. The 0 element of the array contains the complete match, and the 1st to nelement contains any child match in the match. This is equivalentMatchMethod.

If global flag is set for the regular expression,ExecSlaveLastindexTo start searching. If the global flag is not set,ExecIgnoreLastindexFrom the start position of the string.

ExecThe array returned by the method has three attributes:Input,IndexAndLastindex.InputThe attribute contains the entire searched string.IndexThe attribute contains the position of the matched substring in the searched string.LastindexThe attribute contains the next position of the last character in the match.

Example

The example below illustratesExecMethod usage:

 

Code

Function regexptest () {var ver = Number (scriptenginemajorversion () + "." + scriptengineminorversion () if (ver >=5.5) {// test the JScript version. VaR src = "The rain in Spain falls mainly in the plain."; var Re =/\ W +/g; // create the regular expression mode. VaR arr; while (ARR = re.exe C (SRC ))! = NULL) document. write (ARR. index + "-" + arr. lastindex + "\ t" + ARR);} else {alert ("Please use the updated version of JScript ");}}

 

Match Method

UseRegular ExpressionIn this mode, the string is searched, and the result containing the search is returned as an array.

Stringobj.Match (Rgexp)

Function matchdemo () {var R, RE ;//Declare variables.VaR S = "The rain in Spain falls mainly in the plain "; Re =/Ain/ig ;//Create a regular expression mode.R =S. Match (Re);//Try to match the search string.Return (r );//The returned array contains all"Ain" //The four matching results. }

Search Method

Returns the position of the first substring that matches the regular expression.

Stringobj.Search (Rgexp)

Parameters

Stringobj

Required. TheStringObject or string text.

Rgexp

Required. Which contains the regular expression mode and available flagRegular ExpressionObject.

Description

SearchSpecifies whether a matching exists. If a match is found,SearchThe method returns an integer indicating the offset position starting from the string. If no match is found,-1 is returned.

Example

The following example demonstratesSearchMethod usage.

 
Function searchdemo () {var R, RE ;//Declare variables.
 
 VaR S = "The rain in Spain falls mainly in the plain .";Re =/falls/I;
 
//Create a regular expression mode.
 
R =S. Search (Re);
 
//Search for strings.Return (R );
//ReturnBooleanResult .}
Test Method

Returns a Boolean value indicating whether the pattern exists in the searched string.

Rgexp.Test (Str)

Parameters

Rgexp

Required. Which contains the regular expression mode or available flagRegular ExpressionObject.

Str

Required. The string to be searched.

Description

TestMethod to check whether a mode exists in the string. If yes, returnTrueOtherwise, returnFalse.

GlobalRegexpAttributes of an objectTestMethod.

Example

The example below illustratesTestMethod usage:

 
Function testdemo (Re, S) {var S1 ;//Declare variables.
//Check whether a regular expression exists in the string.If (Re. Test (S))
 
//Test whether the API exists.S1 = "contains"; // sInclude mode.
 
ElseS1 = "does not contain"; // sThe exclusive mode.
 
Return ("'" + S + "'" + S1 + "'" + RE. Source + "'");//Returns a string .}
Split Method

Splits a string into substrings and returns the result as a string array.

Stringobj.Split ([Separator [,Limit])

Parameters

Stringobj

Required. To be decomposedStringObject or text. This object will not beSplitMethod modification.

Separator

Optional. String orRegular ExpressionObject that identifies whether one or more characters are used to separate strings. If this option is ignored, a single array of elements containing the entire string is returned.

Limit

Optional. This value is used to limit the number of elements in the returned array.

Description

SplitThe result of the method is a string arrayStingobjAppears inSeparatorThe location must be decomposed.SeparatorIt is not returned as part of any array element.

Example

The following example demonstratesSplitMethod usage.

 
Function splitdemo () {var S, SS;
 
VaR S = "The rain in Spain falls mainly in the plain .";//Separate each space character.
 
Ss =S. Split ("");Return (SS );}
Split Method

Splits a string into substrings and returns the result as a string array.

Stringobj.Split ([Separator [,Limit])

Parameters

Stringobj

Required. To be decomposedStringObject or text. This object will not beSplitMethod modification.

Separator

Optional. String orRegular ExpressionObject that identifies whether one or more characters are used to separate strings. If this option is ignored, a single array of elements containing the entire string is returned.

Limit

Optional. This value is used to limit the number of elements in the returned array.

Description

SplitThe result of the method is a string arrayStingobjAppears inSeparatorThe location must be decomposed.SeparatorIt is not returned as part of any array element.

Example

The following example demonstratesSplitMethod usage.

 
Function splitdemo () {var S, SS; var S = "The rain in Spain falls mainly in the plain .";
 
 //Separate each space character.
 Ss =S. Split ("");Return (SS );}










JS Regular Expression Exec
From: http://www.php100.com/html/webkaifa/javascript/2010/0803/5129.html

// Check whether it is composed of digits

Function isdigit (s)

{

VaR patrn =/^ [0-9] {1, 20} $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Check Logon Name: You can enter only 5-20 strings starting with a letter, which can contain numbers, "_", and ".".

Function isregisterusername (s)

{

VaR patrn =/^ [A-Za-Z] {1} ([a-zA-Z0-9] | [. _]) {} $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Check user name: only 1-30 strings starting with letters can be entered

Function istruename (s)

{

VaR patrn =/^ [A-Za-Z] {1, 30} $ /;

If (! Patrn.exe C (s) return false

Return true

}

}}

// Password verification: only 6-20 letters, numbers, and underscores can be entered

Function ispasswd (s)

{

VaR patrn =/^ (\ W) {6, 20} $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Verify the phone number and fax number. The phone number can start with "+" and contain "-" in addition to numbers.

Function istel (s)

{

// Var patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) {1, 12}) + $ /;

VaR patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Verify the mobile phone number. It must start with a number and can contain "-" in addition to a number.

Function ismobil (s)

{

VaR patrn =/^ [+] {0, 1} (\ D) {1, 3} []? ([-]? (\ D) | []) {1, 12}) + $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Verify the zip code

Function ispostalcode (s)

{

// Var patrn =/^ [a-zA-Z0-9] {3, 12} $ /;

VaR patrn =/^ [a-zA-Z0-9] {} $ /;

If (! Patrn.exe C (s) return false

Return true

}

// Verify the search keyword

Function issearch (s)

{

VaR patrn =/^ [^ '~! @ # $ % ^ & * () + = |\\] [\] \{\}:;' \,. <>/?] {1} [^ '~! @ $ % ^ & () + = |\\]

[\] \{\}:; '\,. <>?] {0, 19} $ /;

If (! Patrn.exe C (s) return false

Return true

}

Function isip (s) // By zergling

{

VaR patrn =/^ [0-9.] {1, 20} $ /;

If (! Patrn.exe C (s) return false

Return true

}

Regular Expression

"^ \ D + $" // non-negative integer (positive integer + 0)

"^ [0-9] * [1-9] [0-9] * $" // positive integer

"^ (-\ D +) | (0 +) $" // non-positive integer (negative integer + 0)

"^-[0-9] * [1-9] [0-9] * $" // negative integer

"^ -? \ D + $ "// integer

"^ \ D + (\. \ D + )? $ "// Non-negative floating point number (Positive floating point number + 0)

"^ ([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"

// Positive floating point number

"^ (-\ D + (\. \ D + )?) | (0 + (\. 0 + )?)) $ "// Non-Positive floating point number (negative floating point number + 0)

"^ (-([0-9] + \\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] * \. [0-9] +) | ([0-9] * [1-9] [0-9] *) $"

// Negative floating point number

"^ (-? \ D +) (\. \ D + )? $ "// Floating point number

"^ [A-Za-Z] + $" // a string consisting of 26 English letters

"^ [A-Z] + $" // a string consisting of 26 uppercase letters

"^ [A-Z] + $" // a string consisting of 26 lowercase letters

"^ [A-Za-z0-9] + $" // string consisting of digits and 26 letters

"^ \ W + $" // a string consisting of a number, 26 English letters, or underscores

"^ [\ W-] + (\\. [\ W-] +) * @ [\ W-] + (\\. [\ W-] +) + $ "// email address

"^ [A-Za-Z] +: // (\ W + (-\ W + )*)(\\. (\ W + (-\ W + )*))*(\\? \ S *)? $ "// URL

"^ [A-Za-z0-9 _] * $"

 


   







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.