html5 replace,JS的replace方法详细介绍

replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数 是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。

下文展示了几种javascript正则表示式的repalce方式,有些方式我们很少在别的地方看到,如第二种和第三方中方法。

//下面的例子用来获取url的两个参数,并返回urlRewrite之前的真实Url

var reg=new RegExp("(http://www.qidian.com/BookReader/)(\\d+),(\\d+).aspx","gmi");

var url="http://www.qidian.com/BookReader/1017141,20361055.aspx";

//方式一,最简单常用的方式

var rep=url.replace(reg,"$1ShowBook.aspx?bookId=$2&chapterId=$3");

alert(rep);

//方式二 ,采用固定参数的回调函数

var rep2=url.replace(reg,function(m,p1,p2,p3){return p1+"ShowBook.aspx?bookId="+p3+"&chapterId="+p3});

alert(rep2);

//方式三,采用非固定参数的回调函数

var rep3=url.replace(reg,function(){var args=arguments; return args[1]+"ShowBook.aspx?bookId="+args[2]+"&chapterId="+args[3];});

alert(rep3);

//方法四

//方式四和方法三很类似, 除了返回替换后的字符串外,还可以单独获取参数

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);

//除了使用replace方法获取正则表示式的分组外,还可以使用test ,exec方法获取分组,只是手法有所不同而已

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="";

//获取所有的分组

for (i = 0; i < m.length; i++) {

s = s + m[i] + "\n";

}

alert(s);

bookId=m[2];

chapterId=m[3];

alert(bookId);

alert(chapterId);

//使用test方法获取分组

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

reg3.test("http://www.qidian.com/BookReader/1017141,20361055.aspx");

//获取三个分组

alert(RegExp.$1);

alert(RegExp.$2);

alert(RegExp.$3);

var str="www.baidu.com";

//str.format("好","q")

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

for(var i=0;i

{

document.write(arguments[i]+"
");

}

document.write("-------------------------------------------------
");

});

两个例子(证明,replace传入正则参数和字符传参数结果不同):

alert("123".replace("1",function(){var un;return un;})); //弹出undefined23

alert("123".replace(new RegExp("1"),function(){var un;return un;})); //弹出23

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值