Beginner JS Regular

Source: Internet
Author: User

The following is only as a personal summary, if wrong, please also point out, thank you

The use of regular expressions is broadly divided into three types:

    1. Determine if a string matches a pattern
    2. Replace the content in a string that conforms to a pattern
    3. extracts the contents of a string that conform to a pattern

JS Create regular expression:

    1. By instantiating a class
      New RegExp (patternattributes);

      Parameter pattern is a string that specifies the pattern of a regular expression or other regular expression

      The parameter attributes is an optional string, called a modifier, that contains the attribute "G", "I", and "M", respectively, for specifying global matching, case-sensitive matching, and multiline matching. The M attribute is not supported until ECMAScript is normalized. If pattern is a regular expression, not a string, you must omit the parameter.

      Note that if pattern is not a valid regular expression, or attributes contains characters other than "G", "I", and "M", the exception hint SyntaxError is thrown; if pattern is a RegExp object, but does not omit the attributes parameter and throws the exception hint TypeError

    2. The literal Way

      /pattern/attributes

JS Regular expression-related properties:

    1. Global identifies whether the regular has a modifier g
    2. IgnoreCase identifies if the regular has a modifier i
    3. Multiline identifies whether the regular has a modifier m
    4. String literals in the source regular expression
    5. LastIndex An integer that marks the beginning of the next matching character position (the value is modified after the test and Exec methods are executed for the next match)

JS Regular Expression Correlation method:

    1. The Compile method is pattern  converted to an internal format, which executes faster. For example, this allows you to use regular expressions more efficiently in loops. When the same expression is reused, the compiled regular expression makes the execution faster. However, this compilation is useless if the regular expression changes.
    2. Test retrieves whether the string matches the regular. The match returns True otherwise false is returned.
    3. EXEC retrieves the value specified in the string. Returns the found value and determines its location. If the regular expression contains a capturing type grouping, the returned value also contains the captured result

Methods for String objects that support regular expressions

    1. Search
    2. Match
    3. Replace
    4. Split

Follow-up content to be perfected ...

Beginner JS Regular

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.