Home > Web Front-end > JS Tutorial > body text

Introduction to javascript regular expressions_javascript skills

WBOY
Release: 2016-05-16 17:47:56
Original
1165 people have browsed it

string object
1, str.match(RegExp)
Search for a string matching RegExp in str and save it in an array and return it,
If RegExp is not a global setting (/g ), match only once

Copy code The code is as follows:

("abc112dwfwabcwef2abc3wfwabcasqf453abcqwf24j234h").match (/abcd*/g);
//Result
["abc112", "abc", "abc3", "abc", "abc"]

In this method , if the regular expression is not a global matching pattern (g tag), the first element will be the matched string, the rest are the strings captured in the regular expression, and the array has 2 attributes:
input for detection String
index matches the starting position of the string used to detect the string.
(For these two attributes, please refer to the RegExp.exec() method)
2. str.search(regExp)
Returns the position of the first string that matches RegExp , if there is no match, return -1. The global tag is meaningless because it is only matched once.
This method also ignores the lastIndex attribute of regExp and always retrieves from the beginning of the string.
3. str.replace(RegExp,replaceText)
Replace the string matching RegExp with replaceText. If RegExp does not have a global setting, it will only match once. Using the global pattern will replace all matching strings. String.
If capture group matching is used in RegExp, $ in replaceText has a special meaning
$1, $2,...,$99 //Text matching the 1st to 99th subexpressions in RegExp .
$& // Substring matching regexp.
$` // Text to the left of the matched substring.
$' // The text to the right of the matched substring.
$$ // Matches the $ symbol itself.
Note that you need to consider the number of capture groups in RegExp. If you only set 2 captures, $3 will no longer have a special meaning
("123ab12c11d_4532").replace(/a(bd*)c (d*)d/,"$1@$2-")
//will get:
"123b12@11-_4532"
4. str.replace(RegExp,function)
The second parameter of str.replace can be a function, and the return value of the function will be used as the replacement content of the matched character.
Note that if you want to globally match RegExp, you still need a global g tag. The parameters of the
function are:
matching string,
configured capture substring (multiple),
starting position of matching string,
original string used for matching
Note, please define the number of function parameters according to the number of captured groups set in RegExp. If there are too few parameters, the "starting position of the matching string" and "the original string used for matching" may not appear in the parameters. , of course, you can also use the arguments object within the function to solve this problem. arguments(arguments.length-2) is the starting position of the matching string, and arguments(arguments.length-1) is the original string used for matching.
Copy code The code is as follows:

var newStr = ("123ab12c11d_4532").replace(/ a(bd*)c(d*)d/g,function(s,s1,s2,pos,oldStr){
return "@" s1 "@" s2 "@";
}) ;
//will get
"123@b12@11@_4532"

5. str.split(RegExp[,limit])
Split the string str into arrays using matching strings. limit is optional and is used to limit the length of the returned array
("ada2afa4fcas6afa").split(/d/,3) // "ada,afa,fcas"
6. RegExp.exec("str") method
in str Find the matching string in. Note that each time you run this method, it only matches once. To match multiple times, you need to set RegExp to /g and run the exec() method multiple times. The return value for each match is result = RegExp.exec( "str")
result is an array, the length of this array is 1, and the array elements are the found matching substrings.
In addition, this array is assigned 2 additional attributes:
result.index Indicates that the matching substring is at the beginning of the original string
result.input is the original string
When no matching substring can be found, return result = null and set RegExp.lastIndex=0
RegExp.lastIndex is an attribute of the regular expression, indicating which position in the string the current match will start from. The initial value is 0.
If RegExp is set to global, after matching a string once, use the same RegExp to match a new string, please manually set RegExp.lastIndex=0 first
If RegExp is not a global matching mode, I wrote another loop in the program to decide whether to terminate the match based on the return value result, thereby trying to match the string. Then, as long as there is a substring that meets the matching conditions, it will inevitably cause an infinite loop, because non-global matching only applies to The string is matched once, and each time the matching operation is run, the first substring is matched, and the returned result is not empty. This is a relatively easy mistake to make.
Copy code The code is as follows:

var str = "1Visit W3School, W3School is a place to study web technology.";
var patt = new RegExp("W3School","g");
var result;
document.write(patt.lastIndex "
");
document.write("=======================================
while ((result = patt.exec(str)) != null) {
document.write(patt.lastIndex "
");
document.write (result.constructor.name "
");
document.write(result.length "
");
document.write(result[0] "< br />");
document.write(result.index "
");
document.write(result.input "
");
document.write("=====================================
") ;
}
document.write(patt.lastIndex "
");
// Operation result:
============== =======================
Array
W3School
Visit W3School, W3School is a place to study web technology.
= ====================================
Array
W3School
Visit W3School, W3School is a place to study web technology.
========================================

7. RegExp.test("str") method
This method is similar to RegExp.exec, except that it only returns true or false
RegExp.lastIndex The meaning is the same (this is a property of RegExp, regardless of whether the test method or the exec method is used)
If the same RegExp uses the test method and the exec method successively, you may need to manually set RegExp.lastIndex=0, these The method is to share the lastIndex attribute of the same RegExp object
Copy the code The code is as follows: str = "1Visit W3School, W3School is a place to study web technology.";
var patt = new RegExp("W3School","g");
var result;
result = patt.test( str);
alert(result); //true
result = patt.test(str);
alert(result); //true
result = patt.test(str);
alert(result); //false


In IE9, newer versions of chrome, and firefox, after str.match(reg) is executed, regardless of whether there is a global match or whether there is a matching result, lastindex is reset, reg.lastIndex = 0, after reg.test(str) is executed, if the regular match is non-global matching, lastindex is reset, re.lastIndex = 0. In IE8 and below, after the regular match is executed, unless there is no matching result , otherwise re.lastIndex is position 1 of the last character matching the end of the string, that is, lastIndex has not been reset.
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!