javascript笔记:String类replace函数的一些事

加固javascript基础知识目的是为以后研究jquery源码做好铺垫。

我最近查阅javascript资料,发现了一个函数:

function format(s)

{

var args = arguments;

var pattern = new regexp("%([1-" + arguments.length + "])","g");

return string(s).replace(pattern,function(word,index){

return args[index];

});

}

// test

window.onload = alert(format("and the %1 want to know whose %2 you %3", "papers", "shirt", "wear"));

//and the papers want to know whose shirt you wear

这种功能的函数,在shell或java都似曾见过,但是在javascript函数实现的方法很新颖。新颖的地方就是在:

return string(s).replace(pattern,function(word,index){

return args[index];

});

但是这里string类的replace的用法和我平时用的很不一样,我以前写过一个这样的replace的函数:

function myreplace(s)

{

return string(s).replace(/cj[0-9]{2}/g,function(word){

return word = 'cjjk00';

});

}

//window.onload = alert(myreplace('cj9080,cj8976,cj12919,cj8765'));//cjjk0080,cjjk0076,cjjk00919,cjjk0065

我在使用replace时候,如果第二个参数是function我一般都只用到第一个参数,基本没有思考它的第二个,第三个或者更多的参数,现在看到有人使用了第二个参数,就很想探求下replace第二个参数使用到了function时候,里面参数到底有多少个,每个的含义到底如何?

下面是我改写了我自己写的替换函数:

function myreplaceftn(s)

{

return string(s).replace(/cj[0-9]{2}/g,function(word,index){

return word = 'cjjk00@' + index + "@";

});

}

//window.onload = alert(myreplaceftn('cj9080,cj8976,cj12919,cj8765'));//cjjk00@0@80,cjjk00@7@76,cjjk00@14@919,cjjk00@22@65

本来我以为,函数format里的function(word,index),我认为应该是正则表达式所匹配字符串的索引(%1的索引为1,%2的索引为2,%3的索引为3),而我写的函数里面第二个参数index不是被匹配到字符串的索引,而是被匹配到的字符在原字符串的位置。下面我做了这样的测试:

function format(s)

{

var args = arguments;

var pattern = new regexp("%([1-" + arguments.length + "])","g");

return string(s).replace(pattern,function(word,index){

alert("arguments.length:" + arguments.length);//4

return args[index];

});

}

function myreplaceftn(s)

{

return string(s).replace(/cj[0-9]{2}/g,function(word,index){

alert("arguments.length:" + arguments.length);//3

return word = 'cjjk00@' + index + "@";

});

}

函数format里面function(word,index)的参数有4个,而函数myreplaceftn(s)里面function(word,index)的参数有3个。为什么会有这样的不同?我做了如下测试:

//以下程序在firefox里面运行

function newformat(s)

{

var args = arguments;

var pattern = new regexp("%([1-" + arguments.length + "])","g");

return string(s).replace(pattern,function(word,index){

console.log("arguments.length:" + arguments.length);

for (var i = 0,j = arguments.length;i

对于回调函数里的arguments值现在比较清晰了,arguments个数的不同应该和我们写的正则表达式有关系,不管怎样,第一个参数是匹配到的字符串,最后一个是原字符串,倒数第二个参数是匹配到的字符串的在原字符串索引的起始位,像format里的第二个参数index根据情况而定了,我自己写的newmyreplace里没有这个参数,format的index参数是%[1-4],里面的1-4,不过还是写个方法确定下:

function charformat(s)

{

var pattern = new regexp("%([a-d])","g");

return string(s).replace(pattern,function(word,index){

switch(index)

{

case 'a':

return 'thisisa';

case 'b':

return 'thisisb';

case 'c':

return 'thisisc';

case 'd':

return 'thisisd';

default:

return 'thisisnull';

}

});

}

window.onload = console.log(charformat("and the %a want to know whose %d you %b", "papers", "shirt", "wear"));

//and the thisisa want to know whose thisisd you thisisb

由此可见string的replace是相当的强大,不过本人正则表达式功力还不够,不知道还有什么别的特别的正则表达式会产生什么不同的结果。另外不知道谁有javascript里面string类replace原始写法,希望能贡献出来,我想好好研究下。


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值