JavaScript learning notes (14) Regular Expression

Source: Internet
Author: User

RegExp class
The RegExp object constructor can contain one or two parameters.
The first parameter describes the pattern string to be matched. If there is a second parameter, this parameter provides additional processing instructions.
I. Basics
1.1 Use RegExp object
Test () method
Test whether the match exists. If a given string (with only one parameter) matches this pattern, true is returned; otherwise, false is returned.
Copy codeThe Code is as follows:
Var sToMatch = "cat ";
Var reCat =/cat/; // Regular Expression literally uses the Perl-style syntax
Alert (reCat. test (sToMatch); // outs "true"

Exec () method
There is a string parameter that returns an array. The first entry in the array is the first match, and the others are reverse references. (That is, the array has only one and is the first matched)
Copy codeThe Code is as follows:
Var strAAA = "a bat, a Cat, a fAt baT, a faT cat ";
Var regAt = new RegExp ("at", "gi ");
Var arr = regAt.exe c (strAAA); // arr [0] is "at", arr. index is 3, and arr. lastIndex is 5
Match () method

Returns an array containing all matching strings.
Var strAAA = "a bat, a Cat, a fAt baT, a faT cat ";
Var regAt = new RegExp ("at", "gi ");
Var arrMatch = strAAA. match (regAt); // Note: String. match (the parameter is a matching character) is opposite to the above
Search () method
Similar to indexOf (), return a matched position in the string. Its parameters are a RegExp object, not just a substring.
Copy codeThe Code is as follows:
Var strAAA = "a bat, a Cat, a fAt baT, a faT cat ";
Var regAt = new RegExp ("at", "gi ");
Var index = strAAA. search (regAt); // outputs "3" the first occurrence location is 3

1.2 string extension method
Replace () method
You can replace the first parameter with the second parameter, and the first parameter can also be a regular expression.
Var strBBB = "The Sky is red .";
// Replace all s in the above sentence and use a regular expression to find all matching
Var strNewBBB = strBBB. replace (/s/gi, "#"); // replace all "s" (regardless of Case sensitivity) ##
Perform another upgrade. The second parameter can also be a function.
Copy codeThe Code is as follows:
Var sToChange = "The sky is red .";
Var reRed =/red /;
Var sResultText = sToChange. replace (reRed, function (sMatch ){
Return "blue ";
});
Alert (sResultText );

In this example, the value of sMatch in the function is always "red" (because this is the only matching mode). The first appearance of "red" is replaced with the return value of the function "blue ".
Additional:
For this sentence in the book "because this is the only matching mode", I think this should be the meaning. replace only has two parameters, and the first parameter is unique, the parameter sMatch of that function should be the value of the first parameter, the unique matching mode...
Split () method
Copy codeThe Code is as follows:
Var sColor = "red, blue, yellow, green ";
Var reComma = /\,/;
Var arrColors = sColor. split (reComma); // split at each comma
Alert (arrColors. length); // outputs "4"

The regular expression reComma must have a backslash before the comma, because the comma has a special meaning in the syntax and must be escaped.
Ii. Simple Mode
2.1 characters
All metacharacters used by regular expressions are:
([{\ ^ $ | )? * +.
A total of 12. Escape these metacharacters at any time, that is, add a backslash to the front.
Example:
Var reQMark = /\? // Escape
Var reQMark = new RegExp ("\\? "); // Note that double escape is required because the backslash itself also needs to be escaped.
So we should try to use the first case, literal syntax in the future! Perl Style
2.2 use special characters
In addition, there are some other predefined special characters, as listed in the following table:
Character Description
----------------------------------------------------
\ T Tab
\ N linefeed
\ R carriage return
\ F page feed
\ A alert character
\ E escape characters
\ Control characters corresponding to cX and X
\ B rollback character
\ V vertical Tab
\ 0 null characters
----------------------------------------------------
2.3 character class
Put some characters in square brackets to effectively tell the regular expression to match the first, second, and third characters.
// ① Character class ---- simple class
Var sToMatch = "a bat, a Cat, a fAt baT, a faT cat ";
// Match the regular expression with bat, cat, or fat
Var reBatCatFat =/[bcf] at/gi;
// Var reBatCatRat =/[\ u0062cf] at/gi; Unicode format
Var arrMatches = sToMatch. match (reBatCatRat );
Alert (arrMatches. join (","); // output "bat, Cat, fAt, baT, faT, cat"
// ② Character class ---- negative class
Var sToMatch = "a bat, a Cat, a fAt baT, a faT cat ";
// Match a regular expression that ends with at but does not start with B or c
Var reBatCatRat =/[^ bc] at/gi; // Escape Character ^ indicates that the character that follows cannot be matched
Var arrMatches = sToMatch. match (reBatCatRat );
Alert (arrMatches. join (","); // output "fAt, faT"
// ③ Character class ---- range class
// Specify the range from a to z: [a-z]. It is case sensitive.
Var sToMatch = "num1, num2, num3, num4, num5, num6, num7, num8, num9 ";
Var reOneToFour =/num [1-4]/gi; // from 1 to 4
Var arrMatches = sToMatch. match (reOneToFour );
Alert (arrMatches. join (","); // output "num1, num2, num3, num4"
// ④ Character class ---- combination class
Combination class is a character class composed of several other classes.
If you want to match all the letters from a-m, numbers from 1 to 4, and a line break, the class used should be like this:
[A-m1-4 \ n]
Do not have spaces between internal classes.
// ⑤ Character class ---- predefine class
Code is equivalent to matching
----------------------------------------------------------------
. [^ \ N \ r] any character except line feed and carriage return
\ D [0-9] Number
\ D [^ 0-9] non-numeric characters
\ S [\ t \ n \ x0B \ f \ r] blank characters
\ S [^ \ t \ n \ x0B \ f \ r] non-blank characters
\ W [a-zA-Z_0-9] Word characters (all characters, numbers, and underscores)
\ W [^ a-zA-Z_0-9] non-word characters
-----------------------------------------------------------------
Using predefined characters can significantly simplify pattern matching. For example, suppose you want to match three numbers:
Var sToMatch = "567 9838 abc ";
Var reThreeNums =/[0-9] [0-9] [0-9]/;
// Var reThreeNums =/\ d/; // The definition is concise.
Alert (reThreeNums. test (sToMatch); // output "true"
2.4 quantifiers
Quantifier can specify the number of occurrences of a specific mode. When specifying the number of times a mode should appear, you can specify the hard quantity or soft quantity.
1. Simple quantifiers
--------------------------------------------------------------------
Code Description
--------------------------------------------------------------------
? Zero or one occurrence
* Zero or multiple occurrences (any)
+ Appears once or multiple times (at least once)
{N} must appear n times
{N, m} appears at least n times, but not more than m times
{N,} appears at least n times
--------------------------------------------------------------------
For example, if you want to match the word bread, read, or red. Using question mark quantifiers, you can use only one expression to match the three:
Var reBreadReadOrRed =/B? Rea? D /;
Or var reBreadReadOrRed =/B {0, 1} rea {0, 1} d /;
2. Greedy, inert, and matching quantifiers
Greedy quantifiers first check whether the entire string matches. If no match is found, it removes the last character from the string and tries again. If no match is found, remove the last character again. This process repeats until a match is found or no character is left in the string.
Lazy quantifiers first check whether the first letter in the string matches. If this character is not enough, read the next character to form a string of two characters. If no match is found, the inert quantizer continues to add characters from the string until it finds a match or the entire string has been checked. The ways in which inertia quantifiers and greedy quantifiers work are the opposite.
The dominant quantizer only attempts to match the entire string. If the entire string cannot be matched, no further attempt is made. In short, the dominant quantizer is a one-size-fits-all approach.
--------------------------------------------------------------------
Greedy inertia control description
--------------------------------------------------------------------
? ?? ? + Zero or one appearance
**? * + Zero or multiple occurrences
++? ++ Appears once or multiple times
{N }? {N} + appears exactly n times
{N, m} {n, m }? {N, m} + appear at least n times at most m times
{N ,}{ n ,}? {N ,}+ appear at least n times
--------------------------------------------------------------------
Let's look at the example below to better understand the above three quantifiers.
Var str = "abbbaabbbaaabbb1234 ";
Var reg1 =/. * bbb/g;
Var reg2 = /.*? Bbb/g;
// Var reg3 =/. * + bbb/g; // an error is reported in Visual Studio2008 .....
Var arrMatches1 = str. match (reg1 );
Var arrMatches2 = str. match (reg2 );
// Var arrMatches3 = str. match (reg3 );
Alert ("greedy:" + arrMatches1.join (",") + "\ n inert:" + arrMatches2.join (","));
The matching process is different!

Related Article

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.