Home Web Front-end JS Tutorial Detailed introduction to the replace method of JS_javascript skills

Detailed introduction to the replace method of JS_javascript skills

May 16, 2016 pm 05:48 PM
js replace

The parameter replacement of the replace() method can be a function instead of a string. In this case, the function is called for each match and the string it returns is used as the replacement text. 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.
//The following example is used to obtain the two parameters of the url and return the real Url before urlRewrite

Copy code The code is as follows:

var reg=new RegExp("(http://www.qidian.com/BookReader/)(\d ),(\d ).aspx","gmi") ;
var url="http://www.qidian.com/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, it can also be used separately Get parameters
Copy code The code is as follows:

var 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 regular expressions, you can also use the test and exec methods to obtain the grouping, but the methods are different
Copy code The code is as follows:

var reg2=new RegExp("(http://www .qidian.com/BookReader/)(\d ),(\d ).aspx","gmi");
var m=reg2.exec("http://www.qidian.com/BookReader/1017141 ,20361055.aspx");
var s="";

//Get all groups
Copy code The code is as follows:

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
Copy the code The code is as follows:

var reg3=new RegExp("(http://www.qidian.com/BookReader/)(\d ),(\d ).aspx","gmi");
reg3.test("http:/ /www.qidian.com/BookReader/1017141,20361055.aspx");

//Get three groups
Copy code The code is as follows:

alert(RegExp.$1);
alert(RegExp.$2);
alert(RegExp.$3);

var str="www.baidu.com";
//str.format("Okay","q")

str.replace(new RegExp("(\.)( bai)du","g"),function(){

for(var i=0;i<arguments.length;i )
{
document.write(arguments[i] "<br/>");
}
document.write("------------------------------- ---------------------<br/>");
});

Two examples (proof, replace The results of entering regular parameters and passing parameters by characters 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
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use JS and Baidu Maps to implement map pan function How to use JS and Baidu Maps to implement map pan function Nov 21, 2023 am 10:00 AM

How to use JS and Baidu Maps to implement map pan function

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts Dec 18, 2023 pm 03:39 PM

PHP and JS Development Tips: Master the Method of Drawing Stock Candle Charts

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to create a stock candlestick chart using PHP and JS

How to use JS and Baidu Maps to implement map polygon drawing function How to use JS and Baidu Maps to implement map polygon drawing function Nov 21, 2023 am 10:53 AM

How to use JS and Baidu Maps to implement map polygon drawing function

How to use JS and Baidu Map to implement map click event processing function How to use JS and Baidu Map to implement map click event processing function Nov 21, 2023 am 11:11 AM

How to use JS and Baidu Map to implement map click event processing function

How to use JS and Baidu Maps to implement map heat map function How to use JS and Baidu Maps to implement map heat map function Nov 21, 2023 am 09:33 AM

How to use JS and Baidu Maps to implement map heat map function

See all articles