Introduction to Javascript Regular Expressions

Source: Internet
Author: User
1) Regular Expressions in Javascript
Javascript uses Regexp objects to represent regular expressions.

2) create a JavaScript Regular Expression
A) Re = new Regexp ("A * B") // use a string as a parameter instead of literals
B) Re =/a * B/
The above two methods are to create a regular expression,/a * B/called the regular expression's Literal Value (literals ).

3) Understand Regular Expressions
A. What is a regular expression?
Javascript Regular Expressions are a subset of Perl regular expressions.
A regular expression is a template for matching (there are many different definitions about regular expressions ). For example,/ABC/is used to match all strings containing ABC.

B. Composition of Regular Expressions
A regular expression is composed of a series of characters, such as/ABC /. Specifically, it includes letters, numbers, escape characters, and punctuation characters. The following lists some common Regular Expression characters:

      1. The numbers and letters match the character itself. For example,/JavaScript/matches a string containing JavaScript.
      2. \ t match the tab (Tab)
      3. \ n match newline (line feed)
      4. \ r match and press ENTER
      5. \ s match null characters
      6. \ s match non-null characters
      7. \ B matches the word boundary, such as/\ bjavascript \ B/matches the string, must contain the word JavaScript, and similar to 123366cript22, such strings cannot match. Note, A word in a word boundary is composed of numbers, letters, or underscores.
      8. \ B matching non-word boundary
      9. ^ $. * +? =! : | \/() [] {}

Reference content above ^ $. * +? =! : | \/() [] {} Is a punctuation character ("and @ are not included). They have special meanings in regular expressions. If we want to match" ABC? ABC "is a problem when it comes to this string, because? It has special meaning, so they need to escape, that is, use \? To replace ?, That is, use/ABC \? ABC/This regular expression is used to match the above string, instead of/ABC? ABC /. Because "there is no special meaning, we do not need to escape it when using it, but if you escape \", it will not affect the result. \ "and" are equivalent, that is, the escape results for characters without special meanings are the same as those for non-escape characters. In general, we do not remember the punctuation characters used in Javascript. As long as they are punctuation characters, we escape them without special meanings.
Due to the complexity of punctuation, we will discuss it here.

C. Significance of punctuation

    1. For character classes, use punctuation [], for example, [ABC]. Note that a character class can only match a character with a length of 1, which is also why it is called a character class.
    [ABC] can match A, B, or C.
    [^ ABC] can match all characters other than A, B, and C.
    [A-z0-9] represents all characters from A to Z and all 0-9 characters. That is, you can use "-" to represent a continuous set of English and numeric characters.
    . It can represent all characters except line breaks.
    \ W: matches all numbers, letters, and "_". It is equivalent to [0-9a-za-z _].
    \ W, W in upper case, matches all characters, excluding numbers, letters, and "_". It is equivalent to [^ 0-9a-za-z _]
    \ D, all numbers, which are equivalent to [0-9]
    \ D: all non-numbers. It is equivalent to [^ 0-9].
    [\ B] indicates the return. Note: If [] is not added, it indicates the word boundary.

    2. Duplicate
    * Indicates that the previous character appears 0 or more times
    +, Indicating that the previous character appears once or more
    ?, Indicates that the previous character appears 0 or 1 time
    {N, m}. The number of repeat characters is between N and M (n <= x <= m)
    {N ,}, the number of repeated characters is greater than or equal to n (x> = N)
    {N}, the number of times the previous character repeats is equal to n (x = N)

    3. Repeated characters
    The character before the above symbol is a repeated character.

    4. Some examples:
    /A */matches "AAA"
    /A */also matches "BBB", because * Indicates repeated 0 or multiple times, so they will match. Note that/A */can match any string.

    5. Greedy repetition (matching) and non-Greedy repetition (matching)
    /A * B/matches "aaaab ". We have said that a * can match a null string or a, AA, AAA, or AAAA. What exactly does it match? In fact, the matching process continues until 4th A, that is, a * matches AAAA, and the matching method is greedy. That is, try to repeat as many repeated characters as possible.
    In *,?, +, {N, m}, {n ,}, {n} followed? It indicates non-Greedy repetition. The so-called non-Greedy repetition means that repeated characters are successfully matched only once. However, a regular expression also has a criterion that must be quasi-searched ------ the sign that the regular expression matches is, find the first matching position in the matched string or there is no matching string. The matching process starts from the first letter of the matched string, when the substring starting with the first letter cannot be matched, the matching starts with the second letter. This sentence is marked as red because it is very important. Correct understanding of this sentence is not only helpful for the following analysis, but also the basis for using JavaScript Regexp objects in the future. Let's take a look at this expression:
    /*? B/obviously, it matches "B". We have already mentioned that non-Greedy repetition is a repeated character that only matches successfully once, that is, *? If a null string is matched, the result is "B ". Program Design, but to verify this problem, here is Code : Program code function f (Regexp, STR ){
    Return Str. Search (Regexp)
    }
    Document. Write (f (/A/, "B") + "<br/> ")
    Document. Write (f (/*? B/, "B") + "<br/> ")
    Document. Write (f (/*? B/, "aaab") + "<br/>") the reference content is displayed.
    -1
    0
    0
    That is to say,/*? B/can match "B", at the same time,/*? B/can also match "aaab ". Isn't this the same as the matching result of/a * B? Actually, because *? After the first successful match, you cannot find a string that can match the regular expression. Therefore, the matching continues until the first matching string "aaab" is found.

    7. Select (alternation), group (grouping), and reference (references)
    |, Select, match the child expression on the left or right of the symbol, for example,/Java | script/can match "Java" or "script". Note, select matching. If the matching on the left is successful, the matching on the right will no longer be performed.

    (), Combination, the expression of the bracket class is considered as an independent unit, so that it can use *, + ,?, | And so on. The main combination can be referenced ;(? :) Is also a form of combination, but such a combination cannot be referenced.

    \ 1, \ 2, and so on, indicating the reference. The number after \ is the group number. The group number is allocated from left to right, that is, the first group starts with a bracket on the left. \ N will match the string of group N that is successfully matched for the first time. For example:
    /(A * B) \ 1/can match aabaab, but cannot match aabab. More complex situations:
    /(ABC (de) \ 1 \ 2/match "abcdeabcdede ". You cannot use references in [], for example,/(A * B) [\ 1]/is invalid.

    8. ^ and $, ^ match the start of a string or the start of a row (in multiline mode), $ match the end of a string or the end of a row (in multiline mode), for example:
    /A * B/match "aaabaab"
    /^ A * B $/does not match "aaabaab"

    9. flags)
    A sign is a simple knowledge point. If we want to match a string containing JavaScript or Javascript, we can use/[JJ] Ava [ss] Ghost/. There is also a method, is to use the flag, that is,/JavaScript/I, I indicates that the matching is case-insensitive. There are also 2 signs, G, M.
    G indicates global match, that is, to find all the strings that can be matched, instead of stopping the string.
    M multi-row mode affects matching of ^ and $, so that ^ matches the beginning of each row, and $ matches the end of each row.
    These three labels can be used together or separately, for example,/ABC/GI/ABC/MI/ABC/GMI.

4) use a regular expression in Javascript
Going from theory to practice is a tough process and you can finally start to use it with excitement.
To construct a Regexp object, we can construct a Regexp object by using the regular expression literals:
Re =/\ bjava \ B/
You can also use the constructor Re = new Regexp ("\ bjava \ B"); to see the difference, the first passed string does not need "/", and secondly, escape the \ in it. This is understandable, because if no escape is provided, the result is "\ bjava \ B", and \ B is a return character in the string.

Method in a. String

    1. Search (). It accepts a string or a Regexp object. If it is a string, it first calls the Regexp constructor and converts it to a Regexp object. Returns the position of the string that is successfully matched for the first time. Therefore, the G flag has no meaning. Example: program code Re =/killercat/I
    STR = "I am killercat"
    Document. Write (Str. Search (re ))
    Result: 5

    2. Replace (), which has two parameters: the first is a regular expression or string, and the second is a string to be replaced. Example: program code Re =/killercat/ig
    STR = "I am killercat"
    Document. Write (Str. Replace (Re, "cat") + "<br/> ")
    Document. Write (STR)
    Result:
    I am Cat
    I am killercat
    Resolution:
    1) if the first parameter is a string, it will not be converted into a regular expression, but will be searched for this string.
    2) returns a new string instead of changing the value of the original string.
    3) You can use g for global matching.

    3. match (). The parameter is the same as search. It can be a string or Regexp object. String is converted to a Regexp object. However, match supports g flag, and match returns an array, for example: (Note that this result is obtained in firefox2.0) the program code RE1 =/*? B/g
    Re2 =/a * B/g
    STR = "aaabaab"
    Arr = Str. Match (RE1)
    For (Ele in ARR ){
    Document. Write (ARR [ele] + "<br/> ")
    }
    Document. Write ("next... <br/> ");
    Arr = Str. Match (re2)
    For (Ele in ARR ){
    Document. Write (ARR [ele] + "<br/> ")
    }
    Result:
    Aaab
    AAB
    Next...
    Aaab
    AAB

    4. split (). The parameter is a string or a regular expression. This parameter acts as a string delimiter and returns an array composed of split substrings, for example: program code RE1 =/\ s +/
    STR = "123 456 789"
    Arr = Str. Split ("");
    For (VAR ele in ARR ){
    Document. Write (ARR [ele] + "<br/> ")
    }
    Document. Write ("next... <br/> ");
    STR = "123 456 789"
    Arr = Str. Split (RE1 );
    For (Ele in ARR ){
    Document. Write (ARR [ele] + "<br/> ")
    }
    Result:
    123
    456
    789
    Next...
    123
    456
    789

B. Regexp object Method

      1. Test (). The parameter is a string. If a match occurs, true is returned. Otherwise, false is the program code Re =/*? B/
      STR = "aaabaab"
      Document. Write (Re. Test (STR ));
      Result: True

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.