js中關(guān)于String對(duì)象的replace使用詳解
更新時(shí)間:2011年05月24日 23:29:35 作者:
關(guān)于String對(duì)象的replace使用詳解,需要的朋友可以參考下。
今天在讀Qwrap的源碼stringH時(shí)里邊有個(gè)
format: function(s, arg0) {
var args = arguments;
return s.replace(/\{(\d+)\}/ig, function(a, b) {
return args[(b | 0) + 1] || '';
});
}
它的使用方式是:
alert(format("{0} love {1}.",'I','You'))//I love you
format的實(shí)現(xiàn)方式主要是用到了String對(duì)象的replace方法:
replace:返回根據(jù)正則表達(dá)式進(jìn)行文字替換后的字符串的復(fù)制。
1.平時(shí)常用到的replace
function ReplaceDemo(){
var r, re; // 聲明變量。
var ss = "The man hit the ball with the bat.\n";
ss += "while the fielder caught the ball with the glove.";
re = /The/g; // 創(chuàng)建正則表達(dá)式模式。
r = ss.replace(re, "A"); // 用 "A" 替換 "The"。
return(r); // 返回替換后的字符串。
}
ReplaceDemo(); //A man hit the ball with the bat. while the fielder caught the ball with the glove.
2.替換模式中的子表達(dá)式
function ReplaceDemo(){
var r, re; // 聲明變量。
var ss = "The rain in Spain falls mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; // 創(chuàng)建正則表達(dá)式模式。
r = ss.replace(re, "$3$2$1"); // 交換每一對(duì)單詞。
return(r); // 返回結(jié)果字符串。
}
document.write(ReplaceDemo()); //rain The Spain in mainly falls the in plain.
匹配正則的項(xiàng):The rain,in Spain,falls mainly,in the;執(zhí)行ss.replace(re, "$3$2$1")操作,完成單詞位置的交換
$1匹配的是第一個(gè)(\S+)
$2匹配的是(\s+)
$3匹配的是第二個(gè)(\S+)
3.replace第二個(gè)參數(shù)是function時(shí)
function f2c(s){
var test = /(\d+(\.\d*)?)F\b/g; // 初始化模式。
return(s.replace(test,function($0,$1,$2){return((($1-32)) + "C");}));
}
f2c("Water boils at 212F 3F .2F 2.2F .2");//Water boils at 180C -29C .-30C -29.8C .2
$0匹配 212F,3F,.2F,2.2F
$1匹配 212,3,.2,2.2
$2匹配 最后一個(gè).2
復(fù)制代碼 代碼如下:
format: function(s, arg0) {
var args = arguments;
return s.replace(/\{(\d+)\}/ig, function(a, b) {
return args[(b | 0) + 1] || '';
});
}
它的使用方式是:
alert(format("{0} love {1}.",'I','You'))//I love you
format的實(shí)現(xiàn)方式主要是用到了String對(duì)象的replace方法:
replace:返回根據(jù)正則表達(dá)式進(jìn)行文字替換后的字符串的復(fù)制。
1.平時(shí)常用到的replace
復(fù)制代碼 代碼如下:
function ReplaceDemo(){
var r, re; // 聲明變量。
var ss = "The man hit the ball with the bat.\n";
ss += "while the fielder caught the ball with the glove.";
re = /The/g; // 創(chuàng)建正則表達(dá)式模式。
r = ss.replace(re, "A"); // 用 "A" 替換 "The"。
return(r); // 返回替換后的字符串。
}
ReplaceDemo(); //A man hit the ball with the bat. while the fielder caught the ball with the glove.
2.替換模式中的子表達(dá)式
復(fù)制代碼 代碼如下:
function ReplaceDemo(){
var r, re; // 聲明變量。
var ss = "The rain in Spain falls mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; // 創(chuàng)建正則表達(dá)式模式。
r = ss.replace(re, "$3$2$1"); // 交換每一對(duì)單詞。
return(r); // 返回結(jié)果字符串。
}
document.write(ReplaceDemo()); //rain The Spain in mainly falls the in plain.
匹配正則的項(xiàng):The rain,in Spain,falls mainly,in the;執(zhí)行ss.replace(re, "$3$2$1")操作,完成單詞位置的交換
$1匹配的是第一個(gè)(\S+)
$2匹配的是(\s+)
$3匹配的是第二個(gè)(\S+)
3.replace第二個(gè)參數(shù)是function時(shí)
復(fù)制代碼 代碼如下:
function f2c(s){
var test = /(\d+(\.\d*)?)F\b/g; // 初始化模式。
return(s.replace(test,function($0,$1,$2){return((($1-32)) + "C");}));
}
f2c("Water boils at 212F 3F .2F 2.2F .2");//Water boils at 180C -29C .-30C -29.8C .2
$0匹配 212F,3F,.2F,2.2F
$1匹配 212,3,.2,2.2
$2匹配 最后一個(gè).2
相關(guān)文章
如何使用uniapp開(kāi)發(fā)微信小程序獲取當(dāng)前位置詳解
uni-app小程序項(xiàng)目無(wú)法直接獲取到地理位置,只能通過(guò)獲取到的經(jīng)緯度,調(diào)用第三方地圖Api獲取,下面這篇文章主要給大家介紹了關(guān)于如何使用uniapp開(kāi)發(fā)微信小程序獲取當(dāng)前位置的相關(guān)資料,需要的朋友可以參考下2022-10-10
javascript實(shí)現(xiàn)遮罩層動(dòng)態(tài)效果實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)遮罩層動(dòng)態(tài)效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
第九篇Bootstrap導(dǎo)航菜單創(chuàng)建步驟詳解
這篇文章主要介紹了Bootstrap導(dǎo)航菜單創(chuàng)建步驟詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

