JS Advanced Programming Learning notes RegExp type

Source: Internet
Author: User

  1. To create a regular expression:
    1. The literal form defines a regular expression:

      The Var expression =/pattern/flags;p attern section allows any simple or complex regular expression to be made. Each regular expression can have one or more flags.

      The flag is: ①g, which represents the global pattern. ②i, which means case insensitive. ③m, representing multiline mode

      The meta-characters of the regular expression include: ([{\ ^ $ |? * +.}]). Note: All meta characters used in the pattern must be escaped (using the \ symbol).

    2. Use the RegExp constructor:

      Var pattern = new RegExp ("Regular expression", "marker"). All two parameters are strings. Sometimes you have to double-escape characters and double-escape all metacharacters.

    3. In ECMASCRIPT3, regular expression literals always share a regexp instance, and every new RegExp instance created with the constructor is a new instance.

      ECMASCRIPT5, it is stipulated that the use of regular expression literals must create a new instance every time, just as the constructor is called directly. Ie9+,ff4+,chrome have made changes.

  2. regexp instance properties
    1. global: Boolean value, Indicates whether the B flag is set.
    2. ignorecase: Boolean value that indicates whether the I flag is set.
    3. lastindex: Integer that represents the character position starting to search for the next occurrence, starting from 0.
    4. multiline: Boolean value that indicates whether the M flag is set.
    5. source: A string representation of a regular expression, returned in literal form rather than in the string pattern in the incoming constructor.
  3. regexp instance method:
    1. var matches = Pattern.exec (text);

      exec (), takes a parameter, which is the string to apply the pattern to, and returns an array containing the first occurrence information , or null if no match is found.

      Even if the global flag is set, only one match will be returned at a time. ① calling the Exec () method multiple times on the same string without setting the global flag will always return information for the first occurrence. ② the global flag is set, each call to EXEC () will continue to look for new items in the string.

      The returned array is an instance of array, but contains two additional attributes: Index and input.

      index represents the position of the match in the string.

      input represents a string that applies a regular expression.

      In the array, the first item is a string that matches the entire pattern, and the other item is a string that matches the capturing group in the pattern (if there is no capturing group in the pattern, the array contains only one item).

    2. test () method, takes a string parameter, returns true if the pattern matches the parameter, otherwise returns FALSE.
    3. tolocalstring () and ToString () return the literal of the regular expression. The
    4. valueof () method returns the regular expression itself.

Iv. RegExp Constructor Properties: P107

JS Advanced Programming Learning notes 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.