Home  >  Article  >  Web Front-end  >  js replace regular expression application case explanation_javascript skills

js replace regular expression application case explanation_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:43:22805browse

var url = "http://www.xxx.com/index.aspx?classid=9&id=2";
To get the tail parameters
Define variables

Copy code The code is as follows:

function parse_url(_url){
//Define function
var pattern = /(w )=( w )/ig;//Define regular expression
var parames = {};//Define array
url.replace(pattern, function(a, b, c){
parames[b] = c;
});
/*This is the most critical. When replace matches classid=9, then execute function(a,b,c); where the value of a is: classid=9 , the value of b is classid, and the value of c is 9; (This is a back reference. Because there are two submatches when defining the regular expression.) Then assign the value of the array key to classid to 9; and then complete. Continue matching until id=2; at this time, execute function (a, b, c); where the value of a is: id=2, the value of b is id, and the value of c is 2; then the key of the array is id The value is assigned to 2. */
return parames;//Return this array.
}
var parames = parse_url(url);
alert(parames['classid'] ", " parames[ 'id']);//Finally print. Print the value corresponding to the array according to the key value
var url = "http://attit.ppk365.com/084200001682"
var reg = /http:// (. )/(w )/ig;
url.replace(reg, function(a,b,c){
alert(a); //Get http://attit.ppk365.com/084200001682
alert(b); //Get attit.ppk365.com
alert(c); //Get 084200001682
});
Statement:
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