JavaScript implements the base regular expression _ regular expression

Source: Internet
Author: User
JavaScript supports regular expressions by regexp classes, for the simplest example:
Copy Code code as follows:

var regapple = new RegExp ("Apple");

It can match the first "Apple" string that appears in a string, and is sensitive to case sensitivity. Adding the second parameter "G" in the constructor represents all of the "Apple" in the search string, where "G" represents "global". If the second argument is "I", the case-insensitive is represented, and the case of the letter is not taken into account in the matching process. By combining the two above, you can search for all the "apple" strings, regardless of the case.
Copy Code code as follows:

var regapple = new RegExp ("Apple", "GI");

Regular expressions have an not unique representation, and using the syntax in the Perl language, you can represent these expressions as:
Copy Code code as follows:

var regapple =/apple/gi;

After creating a RegExp object, the RegExp method can construct different matching methods because the regular expression is an operation on the string, so some of the methods of string play an important role in constructing the regular expression.
Methods of RegExp objects
Copy Code code as follows:

var samplestring = "Greenapple";
var regapple =/apple/;
Alert (Regapple.test (samplestring));

The result of the above code output is "True" because samplestring contains the string "Apple" that needs to be matched, which is the simplest way to detect. Sometimes, we need to know the detailed results of the match, for example:
Copy Code code as follows:

var samplestring = "green apples, red apples";
var regapple =/apple/g;
var arr = regapple.exec (samplestring);

By using the Exec () method, the returned ARR is an array of matching results, including each matching value and the segment it is in, such as "green apples" or "red apples" in the previous example. The match () method has the same functionality as EXEC (), except that it is expressed in a different way:
Copy Code code as follows:

var samplestring = "green apples, red apples";
var regapple =/apple/g;
var arr = Samplestring.match (regapple);

The search () method is similar to the indexof () and returns the location of the first matching string:
Copy Code code as follows:

var samplestring = "green apples, red apples";
var regapple =/apple/gi;
Alert (Samplestring.search (regapple)); Output "6"

Method of String
The replace () method of string can replace the specified string with another string:
Copy Code code as follows:

var samplestring = "There is a green apple";
Alert (Samplestring.replace ("Green", "red")); Output "There is a red apple."

Replace the first parameter of replace () with a regular expression to achieve the same effect:
Copy Code code as follows:

var samplestring = "There is a green apple";
var regapple =/apple/;
Alert (Samplestring.replace (Regapple, "Red")); Output "There is a red apple."

The second parameter of replace () can be replaced with a function () that takes a matching string as a parameter and returns a replacement string. (There are questions)
You can use regular expressions to implement the same functionality as the string split () method.
Copy Code code as follows:

var fruit = "Apple,pear,lemon";
var arr = Fruit.split (",");

Use regular Expressions:
Copy Code code as follows:

var fruit = "Apple,pear,lemon";
var reg =/\,/;
var arr = fruit.split (reg);

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.