"JS Note" 5.4 RegExp type

Source: Internet
Author: User

regexp types contain information: Patterns and flags

To create a RegExp object:

Method One:

Literal form

var expression =/pattern/flags;

Flags

    • G: Global mode, the pattern will be applied to all strings, rather than stopping when the first match is found
    • I: Case insensitive (ignorecase)
    • M: (multiline) multiline mode, which continues to find the next line when it reaches the end of a line of text

Method Two:

REGEXP (patternstring[, flagsstring]) constructors

var pattern2 = new RegExp ("\\[bc\\]at", "I");

Equivalent: var pattern2 =/\[bc\]at/i;

Note:

RegExp objects created in all literal forms in ECMASCRIPT3 share a regexp instance, and RegExp objects created by all constructors each have one regexp instance

ECMAScript5 in two different ways

RegExp Instance Properties

    • Global: Boolean value, whether the G flag is set
    • IgnoreCase: Boolean value. Whether the I flag is set
    • Multiline: Boolean value, whether the M flag is set
    • Source: String. The mode of the RegExp object. (A string in literal form, not a string in the form of a constructor)
    • LastIndex: Integer that represents the starting position of the next search, from zero (test () and exec () with G set, which varies with the use of the RegExp object)

RegExp instance method

EXEC (String)

Returns the array Array,array with the index and input properties

Index property: The starting position of the match to pattern in string

Input:string

The string to match in the array[0]:string

String in array[1],array[2]....:string that matches the capturing group

Note: If the regular Expression object is set to G, then the value of the Lastindex property varies with the multiple calls of the exec ()

Test (String)

If match to pattern in string, return true, modify lastindex;

If no match is reached, return False,lastindex = 0

Inheritance method:

tolocalstring () and ToString (): returns the literal of the RegExp (string), such as: "/cat/i"

valueOf (): returns the regular expression object itself

"JS Note" 5.4 RegExp type

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.