js中关于String对象的replace使用详解
更新时间:2011年05月24日 23:29:35 作者:
关于String对象的replace使用详解,需要的朋友可以参考下。
今天在读Qwrap的源码stringH时里边有个
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的实现方式主要是用到了String对象的replace方法:
replace:返回根据正则表达式进行文字替换后的字符串的复制。
1.平时常用到的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; // 创建正则表达式模式。
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.替换模式中的子表达式
function ReplaceDemo(){
var r, re; // 声明变量。
var ss = "The rain in Spain falls mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; // 创建正则表达式模式。
r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
return(r); // 返回结果字符串。
}
document.write(ReplaceDemo()); //rain The Spain in mainly falls the in plain.
匹配正则的项:The rain,in Spain,falls mainly,in the;执行ss.replace(re, "$3$2$1")操作,完成单词位置的交换
$1匹配的是第一个(\S+)
$2匹配的是(\s+)
$3匹配的是第二个(\S+)
3.replace第二个参数是function时
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匹配 最后一个.2
复制代码 代码如下:
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的实现方式主要是用到了String对象的replace方法:
replace:返回根据正则表达式进行文字替换后的字符串的复制。
1.平时常用到的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; // 创建正则表达式模式。
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.替换模式中的子表达式
复制代码 代码如下:
function ReplaceDemo(){
var r, re; // 声明变量。
var ss = "The rain in Spain falls mainly in the plain.";
re = /(\S+)(\s+)(\S+)/g; // 创建正则表达式模式。
r = ss.replace(re, "$3$2$1"); // 交换每一对单词。
return(r); // 返回结果字符串。
}
document.write(ReplaceDemo()); //rain The Spain in mainly falls the in plain.
匹配正则的项:The rain,in Spain,falls mainly,in the;执行ss.replace(re, "$3$2$1")操作,完成单词位置的交换
$1匹配的是第一个(\S+)
$2匹配的是(\s+)
$3匹配的是第二个(\S+)
3.replace第二个参数是function时
复制代码 代码如下:
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匹配 最后一个.2
相关文章
JavaScript原生对象之Number对象的属性和方法详解
这篇文章主要介绍了JavaScript原生对象之Number对象的属性和方法详解,本文讲解了创建 Number 对象的语法、MAX_VALUE、MIN_VALUE、NaN等属性或方法,需要的朋友可以参考下2015-03-03JavaScript设置、获取、清除单值和多值cookie的方法
cookie 是存储于访问者的计算机中的变量。每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie。你可以使用 JavaScript 来创建和取回 cookie 的值,本文通过一段代码给大家介绍js设置、获取、清除单值和多值cookie的方法,需要的朋友一起学习吧2015-11-11layui 富文本图片上传接口与普通按钮 文件上传接口的例子
今天小编就为大家分享一篇layui 富文本图片上传接口与普通按钮 文件上传接口的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2019-09-09微信小程序实现Session功能及无法获取session问题的解决方法
这篇文章主要介绍了微信小程序实现Session功能及无法获取session问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下2019-05-05
最新评论