JavaScript REGEXP Objects

Source: Internet
Author: User
Tags builtin modifiers

JavascriptRegExp Object

REGEXP: is a shorthand for regular expressions (regular expression).

Complete RegExp Object reference manual

See our reference Manual for JavaScript REGEXP objects, which provides all the properties and methods you can use with string objects.

This manual contains a detailed description and an example of the use of each property and method.

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 an easier way

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

Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:

var=newRegExp("\\w+");  var=/\w+/;         
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 "Runoob" in strings that are case insensitive

var str = "Visit runoob"; var patt1 =/runoob/i;

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

VisitRunoob
Try it»

Example 2

Full text find "is"

var str= "Is this allthere 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 this allthere 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 for the value specified by the string, depending on the result and returns TRUE or false.

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

Instancevar 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»

Regular character escaping rules (preceded by backslash \) are required when creating a regular object using a constructor function

Instancevar re = new RegExp("\ \w+");
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
Try»http://www.runoob.com.

JavaScript REGEXP Objects

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.