"JS" about regular expressions

Source: Internet
Author: User
Tags modifiers

The regular expression describes the pattern object of the character

Grammar:
var patt=New  RegExp (pattern,modifiers); or the simpler way var patt=/pattern/modifiers;
    • The pattern describes an expression model.
    • The modifier (modifiers) describes whether the retrieval is global, case sensitive, and so on.

  Note: When you use constructors to create regular objects, you need a regular character escaping rule (preceded by a backslash \). For example, the following are equivalent:

        var New REGEXP ("\\w+");         var re =/\w+/;   
    • Regular expression modifiers
      • The i -modifier is used to perform a case-insensitive match.
      • The g -modifier is used to perform a full-text search (instead of finding the first one to stop looking, but to find all matches).
    • Method:
      • The test () method searches for the value specified by the string, depending on the result and returns TRUE or false.
        var patt1=New RegExp ("E");d ocument.write (Patt1.test ("The best things on life is Free"); True
      • The EXEC () method retrieves the specified value in the string. The return value is the value that was found. If no match is found, NULL is returned.
        var patt1=New RegExp ("E");d ocument.write (Patt1.exec ("The best things on life is Free");    //e

  • modifier: modifier is used to perform case-sensitive and global matching
    modifier description
    I performs a match that is case insensitive.
    G performs a global match (finds all matches rather than stops after the first match is found).
    m performs multi-line matching.


  • Square brackets: square brackets are used to find characters in a range:
    An expression Description
    [ABC] Look for any characters between square brackets.
    [^ABC] Look for any characters that are not in square brackets.
    [0-9] Look for any number from 0 to 9.
    [A-z] Finds any characters from a to lowercase z from a small write.
    [A-z] Look for any characters from uppercase A to uppercase Z.
    [A-z] Look for any characters from uppercase A to lowercase z.
    [ADGK] Finds any character within a given collection.
    [^ADGK] Finds any character outside the given collection.
    (Red|blue|green) Finds any of the specified options.
  • metacharacters: meta-character (Metacharacter) is a character with a special meaning:
    The
    metacharacters description
    . finds a single character, in addition to line breaks and line terminators.
    \w to find word characters.
    \w to find non-word characters.
    \d to find numbers.
    \d to find non-numeric characters.
    \s to find white space characters.
    \s to find non-whitespace characters.
    \b matches the word boundary.
    \b matches non-word boundaries.
    \ find NUL characters.
    \ n find line breaks.
    \f Find a page break.
    \ r find a carriage return.
    \ t find tabs.
    \v to find vertical tabs.
    \xxx to find characters that are specified in octal number XXX.
    \xdd to find the characters specified in hexadecimal digits dd.
    \uxxxx finds Unicode characters that are specified in hexadecimal number xxxx.
  • quantifier td> matches any string that contains at least one n. The
    quantifier description
    n+
    n matches any string containing 0 or more n.
    n? matches any string that contains 0 or one n.
    n{x} matches a string containing a sequence of X N.
    n{x,y} matches a string containing a sequence of X or Y N.
    n{x,} matches a string containing a sequence of at least X N.
    n$ matches any string that ends with N.
    ^n matches any string that begins with N.
    ? =n matches any string immediately followed by the specified string n.
    ?! n matches any string that is not followed by the specified string n.
  • RegExp Object Method
    method Description FF ie
    compile compiles the regular expression. 1 4
    exec Retrieves the value specified in the string. Returns the found value and determines its location. 1 4
    Test Retrieves the value specified in the string. Returns TRUE or FALSE. 1 4
    About compile method:
    • Feature Description: This method compiles the specified regular expression, and the compiled regular expression The line speed will increase, and if the regular expression is called more than once, then calling the compile method can effectively increase the execution speed of the code and will not have a noticeable effect if the regular expression can only be used once.  
    • basic syntax: Objreg.compile (Pattern[,flag])  
      • objreg required option, regexp object variable name  
      • Pat Tern Mandatory option Regular expression
      • flag option matching option. Optional combinations are:
        • g (Global Search)
        • I (ignoring case)
        • m (Multi-line search)
    • Using the Compile method can also modify and recompile a specified regular expression to improve the adaptability of regular expressions!

  • A String object method that supports regular expressions
    Method Description FF IE
    Search Retrieves a value that matches a regular expression. 1 4
    Match Finds a match for one or more regular expressions. 1 4
    Replace Replace the substring that matches the regular expression. 1 4
    Split Splits a string into an array of strings. 1 4

Reference:

JavaScript REGEXP Objects

JavaScript RegExp Object reference manual

Compile method (regular expression) (JavaScript)

"JS" about regular expressions

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.