Note 007: Object--regexp Regular Expression object

Source: Internet
Author: User

1, REGEXP

    • A pattern text for matching
    • Matching pattern details described with 0 or more modifiers

form of RegExp object creation

    • Create var re = new RegExp ("J.*t") with the built-in constructor
    • The text is defined in the way var re =/j.*t/;

2. RegExp Object Properties

    • G==>global: Whether the related search stops when the first match is found (false default)
    • I==>ignorecase: Ignore case (false default)
    • M==>multiline: Sets the option to search across rows, default to False
    • LastIndex: Index location for search start, default to 0
    • Source: The property used to store the regular expression matching pattern
    • Except for lastindex, objects cannot be modified after they are created.

var re = new RegExp (' j.*t ', ' gmi ');

var re =/j.*t/ig;

2. Methods of RegExp objects

    • Test () returns a Boolean value that is true when matching content is found
    • EXEC () returns an array consisting of matching strings

/j.*t/.test ("Javascript") ==>false

/j.*t/i.test ("JavaScript") ==> true

/j.*t/i.exec ("Javscript") [0] ==> "Javascript"

    • Match (): Returns an array containing the matching contents
    • Search (): Returns where the first match is located
    • Replace (): Replaces the matched text with the specified string
    • Split (): Splits the target string into a number of array elements

var s = new String ("Hellojavascriptworld");

S.match (/a/); ==>["a"]

S.match (/a/g) ==>["A", "a"]

S.replace (/[a-z]/g, ') ==> "Elloavacriptorld"

Use $& to represent matching text when matching objects are found

S.replace (/[a-z]/g, ' _$&') ==> "_hello_java_script_world"//Match text before adding _

If the regular expression is divided into groups (with parentheses), the first group that matches the grouping is used, and so on

S.replace (/([A-z])/g,' _ $ ') ==> "_hello_java_script_world"//Match text before adding _

Note 007: Object--regexp Regular Expression object

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.