JavaScript Advanced Syntax III

Source: Internet
Author: User

A regular expression of JS

1. What is a regular expression

    Regular Expressions (Regular expression) is an object that describes the character pattern, the RegExp class of ECMAScript represents regular expressions, while String and RegExp All define powerful pattern-matching and text-retrieval and substitution functions using regular expressions.

2. How to create a regular expression:

2.1 Way 1:new Way created

var box = new RegExp (' box ', ' IG ');

2.2 Way 2: Create a literal way

var box =/box/ig;

2.3 The first parameter represents the matched string, the second parameter represents the regular match, and the main three match types are:

I: Ignores the size of the matched string.

G: The matched string is globally matched.

M: The matched string is matched in multiple lines.

3, the regular expression matching method:

3.1 Method 1:new () method

3.1.1 Example of the test method using the new operator :

var pattern = new RegExp (' box ', ' I '); Create regular mode, case insensitive

var str = ' This is a box! '; Create a string to compare to

Alert (Pattern.test (str)); the test () method verifies whether a match is returned with a Boolean value of TRUE or Flase

3.1.2 Example of the test method using the literal method :

var pattern =/box/i; Create regular mode, case insensitive

var str = ' This is a box! ';

Alert (Pattern.test (str));

3.1.3 using a single statement for regular matching

Alert (/box/i.test (' This is a box! ')); pattern and string replaced two variables

3.2 Method 2:exec () method

The 3.2.1 method is similar to test ().

var pattern =/box/i; Create regular mode, case insensitive

var str = ' This is a box! '; Create a string to compare to

Alert (pattern.exec (str)); The returned array is matched, otherwise null is returned

      

      

JavaScript Advanced Syntax III

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.