JavaScript REGEXP Objects

Source: Internet
Author: User
Tags modifiers

What is REGEXP?

The regular expression describes the pattern object of the character.

When you retrieve a text, you can use a pattern to describe what you want to retrieve. REGEXP is this pattern.

The simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

You can specify the location of the search in a string, the type of character to retrieve, and so on.

Syntax var patt=new RegExp (pattern,modifiers);

or more simply:

var patt=/pattern/modifiers;
    • The pattern describes an expression model.
    • Modifiers describe whether the retrieval is global, case sensitive, and so on.
REGEXP modifier

Modifiers are used to perform case-insensitive and full-text searches.

The i -modifier is used to perform a case-insensitive match.

The g -modifier is used to perform a full-text search (instead of finding the first one to stop looking, but to find all matches).

Example 1

Find "W3cschool" in strings that are case insensitive

var str= "Visit W3cschool";
var patt1=/w3cschool/i;

The text of the following markup is the matched expression obtained:

VisitW3cschool
Try it»

Example 2

Full text find "is"

var str= "is the There is?";
var patt1=/is/g;

The text of the following markup is the matched expression obtained:

is thAre all there are ?
Try it»

Example 3

Full-text lookup and case insensitive search "is"

var str= "is the There is?";
var patt1=/is/gi;

The text of the following markup is the matched expression obtained:

is th are all there ?
Try it»

Test ()

The test () method searches the string for the specified value, depending on the result and returns TRUE or false.

The following example searches for the character "E" from a string:

Example var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life is free");

Because the letter "E" exists in the string, the output of the above code will be:

True
Try it»

EXEC ()

The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.

The following example searches for the character "E" from a string:

Example 1var patt1=new RegExp ("E");
document.write (Patt1.exec ("The best things in life is free");

Because the letter "E" exists in the string, the output of the above code will be:

E

JavaScript REGEXP Objects

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.