A detailed explanation of JavaScript regular expressions RegExp Object _javascript Skills

Source: Internet
Author: User

I. Overview of RegExp objects
The RegExp object represents a regular expression, and regexp is the abbreviation for a regular expression, which is a powerful tool for matching string execution patterns. The RegExp object is used to specify what is retrieved in the text. When you retrieve a text, you can use a pattern to describe what you want to retrieve. RegExp is the pattern. A simple pattern can be a single character; more complex patterns include more characters and can be used for parsing, formatting checking, replacing, and so on.
A regular expression can specify the location of the retrieval in a string, and the type of character to retrieve.
Second, create Rexexp objects
Creating regular expressions is similar to creating a string, and there are two ways to create regular expressions:
(1) To create the syntax of a RegExp object by using literal quantities:
/pattern/attributes;
(2) Create the syntax of the RegExp object using the New keyword:
New RegExp (pattern, attributes);
Parameter explanation:
1 parameter pattern is a string that specifies the schema or other regular expression of the regular expression.
The 2 parameter attributes is an optional pattern string that contains the attributes "G", "I" and "M", which are used to specify global matches, case-insensitive matches, and multiline matching.
The RegExp object is used to store the retrieval mode. Create a RegExp object with the New keyword. The following generation creates a RegExp object named pattern, whose mode is "E", which is looking for the character "E" when it is retrieved in a string using the RegExp object.

<span style= "FONT-SIZE:18PX;" >var pattern=new RegExp ("E"); 

The above can also be changed to a literal way to create, which is also the way we often use:

<span style= "FONT-SIZE:18PX;" >var pattern=/e/; 
var pattern=/e/gi;</span> 

Three, RegExp object detailed analysis
(1) RegExp Object Properties


These are the basics we've seen in the above examples, but let's take a few simple examples to see:

<span style= "FONT-SIZE:18PX;" >var Pattern=/e/gim; 
document.write (pattern.global+ "");//output: TRUE. The description sets the global mode 
document.write (pattern.ignorecase+ "");//output: True 
document.write (pattern.multiline+ ""); Output: True 
document.write (pattern.source+ "");//Output:e</span> 

(2) RegExp Object method

The RegExp object has 3 methods: Test (), exec (), and compile ().
1 TheTest () method retrieves the specified value in the string, and the return value is true or false.

<span style= "FONT-SIZE:18PX;" >var pattern=/e/; 
var str= "The best things in life are free"; 
document.write (Pattern.test (str));//Output:true</span> 

2 theexec () method retrieves the specified value in the string, the returned value is the found value, and returns null if no match is found.

Instance:

<span style= "FONT-SIZE:18PX;" >var pattern=/e/; 
var str= "The best things in life are free"; 
document.write (Pattern.exec (str));//Output:e</span> 

Instance:
Add a second parameter to the RegExp object to set the retrieval. If you need to find all the existence of a character, you can use the "G" parameter.
When you use the "G" parameter, the EXEC () works as follows:

    • 1 finds the first "E" and stores its location.
    • 2 If you run exec again (), the search starts at the stored location, finds the next "E", and stores its location.
<span style= "FONT-SIZE:18PX;" >var pattern=/e/g; 
var str= "The best things in life are free"; 
Do 
{ 
 var result=pattern.exec (str); 
 document.write (result+ ""); 
} 

The result of the output is: E e e. e e e null
3 thecompile () method is used to change the regular expression, compile () can either change the retrieval mode or add or remove the second parameter.

<span style= "FONT-SIZE:18PX;" >var pattern=/e/; 
var str= "The best things in life are free"; 
document.write (Pattern.test (str));//output: True 
pattern.compile ("D"); 
document.write (Pattern.test (str));//Output:false</span> 

(3) methods that support string objects of regular expressions

Because regular expressions and string objects have a certain connection, some of the methods of a string object can be used for regular expressions:

<span style= "FONT-SIZE:18PX;" >var pattern=/e/g;//Open Global mode 
var str= "The best things in life are free"; 
document.write (Str.match (Pattren) + "<br/>");//output as an array: E,e,e,e,e,e 
document.write (str.search + "<br/>")//Output: 2 (returns the first match to the position) 
document.write (Str.replace (Pattren, "a") + "<br/>");/output: Tha Bast Things in Lifa ara Fraa 
var pattern1=/\s/g;//\s denotes a space character 
document.write (Str.split (PATTREN1));//output: The,best, Things,in,life,are,free</span> 

(4) A meta character is a character that has a special meaning:

Since these are widely used, we just cite a few examples:

<span style= "FONT-SIZE:18PX;" >var pattern=/b.ue/;//dot notation matches any character except a newline. 
var str= "Blue"; 

(5) square brackets are used to find a range of characters:

<span style= "FONT-SIZE:18PX;" >var Pattern=/[a-z]oogle/;//[a-z] represents 26 lowercase letters, and any one can match the 
var str= "Woogle"; 

(6) Classifier

<span style= "FONT-SIZE:18PX;" >var pattern=/go+gle/;//o* indicates matching at least one 0 
var str= "Google"; 

Iv. Common Regular Expressions
The main one is to look at the regular expression represented by the variable Patttern pattern string. The rest are some of the basic things of JS that can be ignored.
(1) Check zip code

<span style= "FONT-SIZE:18PX;" >var pattern=/^[0-9]{6}$/;//must be 6 digits and are all digital 
var str=prompt ("Please enter ZIP Code:"); 
if (Pattern.test (str)) 
{ 
 alert ("You entered the correct postal marking!") "); 
} 
else 
{ 
 alert ("You entered the wrong postal label!") "); 
} </span> 

Enter some data to run the results are:
Input: 056500

Input: 123

(2) Simple email address verification

<span style= "FONT-SIZE:18PX;" >var pattern=/^ ([\w\.\-]+) @ ([\w\-]+) \. ([a-za-z]{2,4}) $/; 
var str=prompt ("Please enter mailbox name:"); 
if (Pattern.test (str)) 
{ 
 alert ("You entered the correct mailbox name!") "); 
} 
else 
{ 
 alert ("You entered the wrong mailbox name!") "); 
} </span> 

(3) Check the upload file compression package

<span style= "FONT-SIZE:18PX;" >var pattern=/[\w]+\.zip|rar|gz/;//\w represents all numbers and letters as well as underlined 
var str=prompt ("Please enter the name of the compressed package:"); 
if (Pattern.test (str)) 
{ 
 alert ("You entered the correct package name!) "); 
} 
else 
{ 
 alert ("You entered the wrong compressed package name!") "); 
} </span> 

(4) Check mobile phone number

<span style= "FONT-SIZE:18PX;" >var pattern=/^[1][0-9]{10}$/; 
var str=prompt ("Please enter your mobile number:"); 
if (Pattern.test (str)) 
{ 
 alert ("You entered the correct cell phone number!") "); 
} 
else 
{ 
 alert ("You entered the wrong cell phone number!") "); 
} </span> 

The results of the three output below are no longer displayed in one by one, as long as the pattern regular expression is written to verify that the data entered is correct. Because just contact regular expression, may have incorrect place, oneself will carry on perfect and revise, hope to be helpful to everybody's study.

Related Article

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.