Regexp Regular Expression

Source: Internet
Author: User

The Regexp object is used to specify the content to be retrieved in the text.

What is Regexp?

Regexp is the abbreviation of a regular expression.

When you retrieve a text, you can use a mode to describe the content to be retrieved. Regexp is in this mode.

A simple mode can be a single character.

More complex modes include more characters and can be used for parsing, format check, replacement, and so on.

You can specify the search position in the string and the type of characters to be retrieved.

Define Regexp

The Regexp object is used to store the retrieval mode.

Use the New Keyword to define the Regexp object. The following code defines the Regexp object named patt1, and its pattern is "E ":

var patt1=new RegExp("e");

When you use this Regexp object to search in a string, the character "E" will be searched ".

Regexp object Method

The Regexp object has three methods: Test (), exec (), and compile ().

Test ()

The test () method retrieves the specified value in the string. The return value is true or false.

Example:
var patt1=new RegExp("e");document.write(patt1.test("The best things in life are free")); 

Because the character string contains the letter "E", the output of the above Code will be:

true
Exec ()

The Exec () method retrieves the specified value in the string. The returned value is the value found. If no matching is found, null is returned.

Example 1:
var patt1=new RegExp("e");document.write(patt1.exec("The best things in life are free")); 

Because the character string contains the letter "E", the output of the above Code will be:

e
Example 2:

You can add the second parameter to the Regexp object to set the search. For example, you can use the "G" parameter ("Global") if you want to find all the characters that exist ").

When the "G" parameter is used, exec () works as follows:

  • Locate the first "E" and store its location
  • If exec () is run again, search from the storage location, find the next "E", and store its location
var patt1=new RegExp("e","g");do{result=patt1.exec("The best things in life are free");document.write(result);}while (result!=null) 

The code output will be:

eeeeeenull
Compile ()

The compile () method is used to change Regexp.

Compile () can either change the search mode or add or delete the second parameter.

Example:
var patt1=new RegExp("e");document.write(patt1.test("The best things in life are free"));patt1.compile("d");document.write(patt1.test("The best things in life are free"));

The output of the above Code is:

truefalse

Regexp Regular Expression

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.