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

Introduction to the replace method of JS_javascript skills

WBOY
Release: 2016-05-16 17:48:57
Original
1050 people have browsed it

The first parameter of this function is a string matching the pattern. The next argument is a string that matches the subexpression in the pattern. There can be 0 or more such arguments. The next parameter is an integer that declares the position in the stringObject where the match occurs. The last parameter is the stringObject itself.
The following shows several ways to repalce javascript regular expressions. Some ways we rarely see elsewhere, such as the second and third-party methods.

Copy code The code is as follows:

//The following example is used to obtain the two URLs Parameters and return the real Url before urlRewrite
var reg=new RegExp("(http://www.jb51.net/BookReader/)(\d ),(\d ).aspx","gmi") ;
var url="http://www.jb51.net/BookReader/1017141,20361055.aspx";
//Method 1, the simplest and most commonly used method
var rep=url.replace( reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");
alert(rep);
//Method 2, using a callback function with fixed parameters
var rep2=url.replace(reg ,function(m,p1,p2,p3){return p1 "ShowBook.aspx?bookId=" p3 "&chapterId=" p3});
alert(rep2);
//Method 3, using non-fixed Parameter callback function
var rep3=url.replace(reg,function(){var args=arguments; return args[1] "ShowBook.aspx?bookId=" args[2] "&chapterId=" args[3] ;});
alert(rep3);
//Method 4
//Method 4 is very similar to method 3. In addition to returning the replaced string, you can also obtain the parameter
var separately bookId;
var chapterId;
function capText()
{
var args=arguments;
bookId=args[2];
chapterId=args[3];
return args[1] "ShowBook.aspx?bookId=" args[2] "&chapterId=" args[3];
}
var rep4=url.replace(reg,capText);
alert( rep4);
alert(bookId);
alert(chapterId);
//In addition to using the replace method to obtain the grouping of the regular expression, you can also use the test and exec methods to obtain the grouping, but the method is different It's just different
var reg2=new RegExp("(http://www.jb51.net/BookReader/)(\d ),(\d ).aspx","gmi");
var m= reg2.exec("http://www.jb51.net/BookReader/1017141,20361055.aspx");
var s="";
//Get all groups
for (i = 0; i < m.length; i ) {
s = s m[i] "n";
}
alert(s);
bookId=m[2];
chapterId=m[3];
alert(bookId);
alert(chapterId);
//Use the test method to get the group
var reg3=new RegExp("(http://www. jb51.net/BookReader/)(\d ),(\d ).aspx","gmi");
reg3.test("http://www.jb51.net/BookReader/1017141,20361055.aspx ");
//Get three groups
alert(RegExp.$1);
alert(RegExp.$2);
alert(RegExp.$3);
var str="www .baidu.com";
//str.format("good","q")
str.replace(new RegExp("(\.)(bai)du","g"),function (){
for(var i=0;i{
document.write(arguments[i] "
");
}
document.write("--------------------------------------------- -------
");
});
Two examples (to prove that the results of replacing regular parameters and character parameters are different):
alert(" 123".replace("1",function(){var un;return un;})); //pop up undefined23
alert("123".replace(new RegExp("1"),function(){ var un;return un;})); //pop up 23
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!