Overview of Regular Expression in JS

Source: Internet
Author: User

Preface:

In the previous blog titled "use of control Verification in ASP. NET", its regularexpressvalidation verification control applies a regular expression, but encapsulates a regular expression. Here we will give an overview of the regular expression.

 

Create a regular expression:

Regular ExpressionIt is a text mode consisting of common characters (for example, from A to Z) and special characters (called metacharacters. Match it with the string to obtain the result (for details, we will continue later ...)

First, let's look at the basic expression creation method.

// Description: Creates a regular expression.

// Copyright: http://www.cnblogs.com/yangmingming

// Notes: basic creation method demonstration

VaR Re = New Regexp ();
// Regexp is a class in JS, similar to array. However, this creation method does not specify the expression content
Re = New Regexp ( " A " );
// The simplest regular expression that matches the letter
Re = New Regexp ( " A " , " I " );
// The second parameter of the overloaded constructor is case insensitive.

The second parameter is an optional parameter, which includes three types:

    • G: full-text search;
    • I: case insensitive;
    • M: multi-row search;

However, the more common Regular Expression creation rules are:The literal declaration method.That is:

// Description: Creates a regular expression.

// Copyright: http://www.cnblogs.com/yangmingming

// Notes: basic creation method demonstration

VaR Re = / A / I;
// It works the same way: Re = new Regexp ("A", "I"), and is more commonly used.

Regular Expression Methods and attributes:

 

Describes the common methods and attributes of regular expressions and the matching method of related strings.

Regular Expression Method:

    • Test method: Return boolean type. It indicates whether the searched string contains the pattern. If yes, true is returned; otherwise, false is returned;
    • Exec method: use the regular expression mode to search for strings and return an array that matches the regular expression mode;

 Regular Expression attributes:

    • Source attribute: returns the text content of the regular expression. Read-only;

 String Matching regular expression method:

    • Match Method: finds matching of one or more regular expressions. Similar to the exec method, an array is returned.

Examples of the above methods and PropertiesCodeAs follows:

 

Code // Description: Creates a regular expression.

// Copyright: http://www.cnblogs.com/yangmingming

// Notes: basic creation method demonstration

// Test method:
VaR Re = / ^ [A-Z] + \ s + \ D + $ / I; // + Indicates that the character appears at least once, \ s indicates a blank character, and \ D indicates a number.
VaR Osversion = " Ubuntu 8 " ;
Alert (Re. Test (osversion )); // True;

// Test the exec method. It is special. Its index No. 1st starts and contains child matching elements.
VaR Re = / ^ [A-Z] + \ s + (\ D +) $ / I; // The sub-match is defined;
VaR Arr = Re.exe C (osversion );
Alert (ARR [ 0 ]); // Complete osversion string;
Alert (ARR [ 1 ]); // 8;

// Test the match method.
VaR Str = " My name is yangmingming " ;
Re = / A-Z / G; // G indicates the global variable. Otherwise, the array length is 1;
VaR Arr = Str. Match (re );
Alert (ARR ); // Output m, n, I, Y;

// Test Property Source
VaR Re = / [A-Z] / I;
Alert (Re. source ); // Output [A-Z];

The test method of the regular expression is commonly used, because most users only need to know whether a string matches a regular expression. True is true; otherwise, false is used;

 

Application of Regular Expressions in JS:

Previous Article about Verifying controlsArticleThe regarexpressvalidation control in can be easily implemented to verify whether the content of the entered text box conforms to the valid data. However, here we apply the application of regular expressions directly in Js.

 Environment layout:

Set the simplest email text input box and a submit button. If the input format is invalid, alert returns false; otherwise, true is returned;

 

< Head runat = " Server " >
< Title > Regular Expression demo page < / Title>
< Script Type = " Text/JavaScript " Language = " Javascript "   >
// Description: A simple example that demonstrates the application of regular expressions in JavaScript.

// Copyright: http://www.cnblogs.com/yangmingming

// Notes: when the input does not match, alert returns false;

Function Btnsubmit ()
{
VaR Re =   / ^ [A-zA-Z0-9 _-] + @ [a-zA-Z0-9 _-] + (\. [a-zA-Z0-9 _-] +) + $ / ;// Regular Expression of email
VaR Txtmail = Document. getelementbyid ( " Txtmail " ). Value;
If ( ! Re. Test (txtmail ))
{
Alert ( " Invalid email format! " );
Return   False ;
}
Else  
Return   True ;
}
< / SCRIPT>
< / Head>
< Body >
< Form ID = " Form1 " Runat = " Server " >
< Div >
Email: < ASP: textbox ID = " Txtmail " Runat = " Server " > < / ASP: textbox>
< BR / >
< ASP: button ID = " Btnsubmit " Runat = " Server " Text = " Submit "
Onclientclick = " Return btnsubmit (); " Onclick = " Btnsubmit_click "/ >
< / Div>
< / Form>
< / Body>
< / HTML>

When the input does not match, It is shown as follows:

 

 

In summary, the regular expression is outlined. However, it is a very wide concept and cannot be comprehensive. In the end, the actual example is to apply a regular expression to verify the use of the control in JS to further understand it. Haha ~

 

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.