JavaScript REGEXP Objects

Source: Internet
Author: User
Tags modifiers

1. Definition: When detecting a text, you can use a pattern to describe what you want to detect, RegExp is this pattern.

2. Syntax: var patt=new RegExp (pattern,modifiers), or Var patt=/pattern/modifiers;

The pattern describes an expression model that describes whether the retrieval is global and case-sensitive.

3.REGEXP modifier to perform a case-insensitive match.

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:

Visit W3cschool

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 th are allthere ?

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 allthere ?

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:

Instance

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

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

Instance

var re = new RegExp ("\\w+");

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 1

var 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

Replication replication: https://www.2cto.com/kf/201601/487274.html

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.