Regular expression method
A, test methods
Whether there is a match for the string regular expression pattern of the test string parameter
If there are returns true, false otherwise
Example:
1, using the test method flag is not set g
2, using the test method set globally g
Value becomes unstable, will be a true one will be flase. (In fact, it is the reason lastIndex, not every match is starting from scratch)
The first lastIndex = 0 (starting after the first 0, returns true) after the lookup, will lastIndex value as an index into a position matching the last character of a string of +1,
Second, if there is lastIndex will start from this position when it returns true, false otherwise find, lastIndex = 0 + 1 (starting after the first one, the return true)
Third lookup lastIndex = 1 + 1 (starting after the second, return false)
I would like to use the original intent of the test method is necessary to increase the g flag
Two, exec () method
Objective: Adaptation regular expression pattern string search is performed and update the properties of the global RegExp object matching result to the reaction
If no matching text is returned null, if the match to return a result array;
Contents of the array:
- Position of the first character of the index matching statement text (the first few characters from the start of the match)
- storing input character string retrieved string
Non-Global call
Call non-global RegExp object exec (time), returns an array of matches
The first element is the regular expression to match the text
The second element is the first subexpression matches RegExpObject text (if any)
The third element is the second sub-expression RegExp object that matches the text (if any), and so on
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title></title>
6 </head>
7 <body>
8 <script>
9 var reg1=/\d(\w)\d/g;
10 var reg2=/\d(\w)\d/;
11 var as="a1b2c3d4e5";
12
13 var sd=reg1.exec(as);
14 document.write(sd.index + sd.toString());
15 console.log(reg1.lastIndex + '\t' + sd.index + '\t' + sd.toString());
16 console.log(reg1.lastIndex + '\t' + sd.index + '\t' +sd.toString());
17
18 </script>
19 </body>
20 </html>
Regular Expression object methods
A, search () method
Objective: to search character string specified in the substring, or retrieve the sub-string that matches a regular expression
Method returns the first match results index (position of the first character matched occur), look less than -1
search () method does not perform a global match, ignore flag g, and always retrieved from the beginning of the string
Example:
Direct digital still write
Two, match () method
Objective: match () method retrieves the character string, or to find a plurality of matching regexp text
And search () ignores g mark is different, match () method regexp has the g flag if there is a great impact on the results
Non-Global call
If there is no sign regexp g, then the match () method can only perform a match in a string
If no text match is not found, returns null
If a match is found the text will return an array to store the information it finds matching text related.
It returns the first element of the array is stored in the matching text, while the remaining elements are stored in the regular expression matching sub-expressions of the text
In addition to the conventional elements of the array, the array also contains a return object properties 2
- Statement index matching text characters starting position of the string
- input reference to the statement of stringObject
var reg3=/\d(\w)\d/g;
var ts="a1b2c3d4e5";
var ret=ts.match(reg3);
console.log(ret);
console.log(reg3.lastIndex + '\t' + ret.index + '\t' );
Global call
If the flag g regexp having the match () method to perform a global search, find all matching substring string
- Did not find any matching substring, null is returned
- If one or more matching substring, it returns an array
China array element is stored in all the string matching substring, and there is no input property or properties indx (less amount of information)
var reg4=/\d(\w)\d/;
var ts="a1b2c3d4e5";
var ret=ts.match(reg4);
console.log(ret);
console.log(reg4.lastIndex + '\t' + ret.index + '\t' )
Three, split () method
We often use the split method to split a string into an array of characters
In the case of some complex division can use regular expressions to solve
Digital sign as a separator
Four, replace () method
Grammatical structures:
String.prototype.replace(str,replaceStr)
String.prototype.replace(reg,replaceStr)
String.prototype.replace(reg,function)
第一个参数可以是字符串或正则表达式,第二个参数可以是需要替换的字符也可以是函数
例:查找字符串中的数字,并替换成数字或字母
String.prototype.replace(reg,function)
function参数含义
function会在每次匹配替换的时候调用(每次匹配的时候就调用一次),调用的时候会传入四个参数
- 匹配字符串
- 正则表达式分组内容,没有分组没有该参数
- 匹配项在字符串中的index(每个匹配结果的位置)
- 原字符串
实例:
1、
'a1b2c3d4e5'.replace(/\d/g, function(match,index,origin){
console.log(index);
return parseInt(match) + 1;
});
2、
'a1b2c3d4e5'.replace(/(\d)(\w)(\d)/g, function(match,group1,group2,group3,index,origin) {
console.log(match);
return group1 + group2;
});