RegExp class
The constructor of the RegExp object can take one or two parameters
The first parameter describes the pattern string that needs to be matched. If there is a second parameter, this parameter has additional processing instructions.
1. Basics
1.1 Using RegExp Object
test() method
Test whether it matches. If the given string (only one parameter) matches this pattern, it returns true, otherwise it returns false
Copy the codeThe code is as follows:
var sToMatch = "cat";
var reCat = /cat/; // Regular expression literals use Perl-style syntax
alert((sToMatch)); //outs "true"
exec() method
There is a string parameter that returns an array. The first entry in the array is the first match, the others are backreferences. (i.e., there is only one in the array and it is the first match)
Copy the 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 = (strAAA); //arr[0] is "at", the value is 3, and the value is 5
match() method
Returns an array of matches contained in the string.
var strAAA = "a bat, a Cat, a fAt baT, a faT cat";
var regAt = new RegExp("at", "gi");
var arrMatch = (regAt); //Note: string.match (parameters are matching characters) is the opposite of the above
search() method
Somewhat similar to indexOf(), returns a matching position that appears in the string. Its argument is a RegExp object rather than just a substring.
Copy the 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 = (regAt); //outputs "3" The first occurrence position is 3
1.2 Extended string method
replace() method
The first parameter can be replaced 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 regular expression to find all matching
var strNewBBB = (/s/gi, "##"); //Replace all "s" (regardless of upper and lower case) with ##
To upgrade, the second parameter can also be a function
Copy the codeThe code is as follows:
var sToChange = "The sky is red.";
var reRed = /red/;
var sResultText = (reRed, function(sMatch) {
return "blue";
});
alert(sResultText);
In this example, the value of sMatch in the function is always "red" (because this is the unique matching pattern). The first occurrence of "red" is replaced by the return value of "blue" of the function.
Attached:
Regarding the sentence in the book, "Because this is the only matching pattern" I think it should mean this. Replace only has two parameters, the first parameter is found to be unique, and the parameter sMatch of the function should be the value of the first parameter in the previous parameter, the unique matching pattern. . .
split() method
Copy the codeThe code is as follows:
var sColor = "red,blue,yellow,green";
var reComma = /\,/;
var arrColors = (reComma); //split at each comma
alert(); //outputs "4"
There must be a backslash in the regular expression reComma before the comma, because commas have special meanings in the syntax and must be escaped.
2. Simple mode
2.1 element characters
All meta characters used in regular expressions are:
( [ { \ ^ $ | ) ? * + .
There are 12 in total. At any time when these meta characters are used, you need to escape, that is, add a backslash in front of it.
example:
var reQMark = /\?/; //Escape
var reQMark=new RegExp("\\?"); //Note here that double escapes, because the backslash itself also needs to be escaped
So we should try to use the first case in the future, literal syntax! Perl's style
2.2 Use special characters
In addition, there are some other predefined special characters, listed in the following table:
Character Description
----------------------------------------------------
\t Tab characters
\n Line break
\r Carriage return character
\f Page break
\a alert character
\e escape character
\cX The control character corresponding to X
\b Backup character
\v Vertical tab character
\0 null character
----------------------------------------------------
2.3 Character Class
Putting some characters into square brackets can effectively tell the regular expression to match the first, second, third characters, etc.
//① Character class----Simple class
var sToMatch = "a bat,a Cat,a fAt baT,a faT cat";
//Match regular expressions with bat or cat or fat
var reBatCatFat = /[bcf]at/gi;
//var reBatCatRat=/[\u0062cf]at/gi; Use Unicode form
var arrMatches = (reBatCatRat);
alert((",")); //Output "bat,Cat,fAt,baT,faT,cat"
//② Character class----Negative class
var sToMatch = "a bat,a Cat,a fAt baT,a faT cat";
//Match regular expressions ending with at but not starting with b or c
var reBatCatRat = /[^bc]at/gi; //De-character^ means that the following characters cannot be matched
var arrMatches = (reBatCatRat);
alert((",")); //Output "fAt,faT"
//③ Character class----Scope class
//Specify the range from a to z: [a-z]. Here 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 = (reOneToFour);
alert((",")); //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 letters from a-m and numbers from 1-4, and a newline character, then the class used should be like this:
[a-m1-4\n]
Be careful not to have spaces between the internal classes.
//⑤ Character class----Predefined class
Code is equivalent to matching
----------------------------------------------------------------
. [^\n\r] Any character except line breaks and carriage return
\d [0-9] Number
\D [^0-9] Non-numeric characters
\s [ \t\n\x0B\f\r] Whitespace character
\S [^ \t\n\x0B\f\r] Non-whitespace character
\w [a-zA-Z_0-9] Word characters (all characters, numbers and underscores)
\W [^a-zA-Z_0-9] Non-word character
-----------------------------------------------------------------
Using predefined characters can obviously make pattern matching simple. For example, suppose to match 3 numbers:
var sToMatch = "567 9838 abc";
var reThreeNums = /[0-9][0-9][0-9]/;
//var reThreeNums=/\d\d\d/; //Be more concise with predefined
alert((sToMatch)); //Output "true"
2.4 Quantitative Words
Quantifiers can specify the number of times a certain pattern occurs. When specifying the number of times a certain mode should occur, you can specify the hard number or the soft number.
1. Simple Measurement Words
--------------------------------------------------------------------
Code Description
--------------------------------------------------------------------
? Zero or once
* Zero or multiple occurrences (any time)
+ Appear once or more 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, suppose to match the words bread, read, or red. Using a question mark quantifier, you can match these three just by using one expression:
var reBreadReadOrRed = /b?rea?d/;
or var reBreadReadOrRed = /b{0,1}rea{0,1}d/;
2. Greedy, lazy and dominant quantifiers
Greedy quantifiers first check whether the entire string matches. If no match is found, it removes the last character in the string and tries again. If no match is found, then remove the last character again, and the process will be repeated until a match is found or the string has no characters left.
Lazy quantifiers first look at whether the first letter in the string matches. If this character is not enough, read the next character to form a two-character string. If no match is found, the lazy quantifier continues to add characters from the string until the match is found or the entire string has been checked and there is no match. Lazy quantifiers work exactly the opposite way.
Dominant quantifiers try to match the entire string only. If the entire string does not produce a match, no further attempt is made. In fact, to put it simply, the dominant quantifier is one-size-fits-all.
--------------------------------------------------------------------
Greedy, lazy, dominance, description
--------------------------------------------------------------------
? ?? ?+ Zero or once
* *? *+ Zero or multiple occurrences
+ +? ++ Appears once or more
{n} {n}? {n}+ appears exactly n times
{n,m} {n,m}? {n,m}+ At least n times at most m times
{n,} {n,}? {n,}+ appears at least n times
--------------------------------------------------------------------
See the following examples 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 = (reg1);
var arrMatches2 = (reg2);
//var arrMatches3 = (reg3);
alert("greedy:" + (",") + "\nLaised:" + (","));
The main thing is that the matching process is different!