Home > Web Front-end > JS Tutorial > JavaScript Study Notes (14) Regular Expressions_Basic Knowledge

JavaScript Study Notes (14) Regular Expressions_Basic Knowledge

WBOY
Release: 2016-05-16 18:36:03
Original
1164 people have browsed it

RegExp class
The constructor of the RegExp object can take one or two parameters
The first parameter is a pattern string that describes the pattern that needs to be matched. If there is a second parameter, this parameter is specified Additional processing instructions.
1. Basics
1.1 Use the RegExp object
test() method
to test whether it matches. If the given string (only one parameter) matches this pattern, it returns true, otherwise it returns false

Copy code The code is as follows :

var sToMatch = "cat";
var reCat = /cat/; //Regular expression literals use Perl-style syntax
alert(reCat.test(sToMatch) ); //outs "true"

exec() method
has a string parameter and returns an array. The first entry in the array is the first match, the others are backreferences. (That is, there is only one in the array, and it is the first matching one)
Copy code The 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.exec( strAAA); //arr[0] is "at", arr.index value is 3, arr.lastIndex value is 5
match() method

returns a string contained in An array of all matches.
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 the matching character) is the opposite of the above
search() method
is somewhat similar to indexOf(), returning the values ​​that appear in the string A matching position. Its parameter is a RegExp object rather than just a substring.
Copy code The 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 position is 3

1.2 Extended string method
replace() method
can replace the first parameter with the second parameter, and the first parameter here can also be a regular expression Mode.
var strBBB = "The Sky is red.";
//Replace all s in the above sentence and use regular expressions to find all matching
var strNewBBB = strBBB.replace(/ s/gi, "##"); //Replace all "s" (regardless of case) with ##
and then upgrade it. The second parameter can also be a function
Copy code The 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 pattern). The first occurrence of "red" is replaced with the function's return value "blue".
Append :
I think this is what the sentence in the book "because this is the only matching pattern" means. replace has only two parameters. The first parameter is the only one found. The parameter sMatch of that function should be It is the value of the previous first parameter, the only matching pattern. . .
split() method
Copy code The 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"

There must be a backslash before the comma in the regular expression reComma, because the comma has a special meaning in the grammar and must be escaped.
2. Simple mode
2.1 Metacharacters
All metacharacters used in regular expressions are:
( [ { ^ $ | ) ? * .
Total 12. Anytime these metacharacters are used they need to be escaped, that is, preceded by a backslash.
Example:
var reQMark = /?/; //Escape
var reQMark=new RegExp("\?"); //You need to pay attention here, double escaping, because the backslash itself is also
needs to be escaped, so we should try to use the first case, literal syntax, in the future! Perl style
2.2 Using special characters
In addition, there are some other predefined special characters, as listed in the following table:
Character Description
------ -----------------------------------------------
t Tab character
n Line feed character
r Carriage return character
f Page feed character
a alert character
e escape character
cX Control character corresponding to X
b Fallback character
v Vertical tab character

The dominant quantifier only attempts to match the entire string. If the entire string does not produce a match, no further attempts are made. In fact, the dominant quantifier is, simply put, one size fits all.
-------------------------------------------------- --------------------------
Description of greed and laziness dominance
------------------ --------------------------------------------------
? ?? ? Zero or one occurrences
* *? * Zero or more occurrences
? One or more occurrences
{n} {n}? {n} Exactly n occurrences
{n,m} {n,m}? {n,m} appears at least n times and at most m times
{n,} {n,}? {n,} appears at least n times
- -------------------------------------------------- ------------------
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; //Error reported in Visual Studio2008....
var arrMatches1 = str.match(reg1);
var arrMatches2 = str.match(reg2);
//var arrMatches3 = str.match(reg3);
alert("Greedy:" arrMatches1.join( ",") "nLazy:" arrMatches2.join(","));
The main difference is the matching process!
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template