js replace常用用法
2013-04-15 20:21
295 查看
1. 最简单 var template = "this is a {img}"; template = template.replace("{img}", "http://xxx"); //this is a http://xxx 2. 如果template中有两个同样的? var template = "this is a {img}, this {img} is beautiful"; template = template.replace(/{img}/g, "http://xxx"); //this is a http://xxx, this http://xxx is beautiful 3. 如果"key"是不确定的? var config = { "img" : "http://xxx", "title" : "pig" } var template = "this is a {title}'s {img}, this {img} is beautiful"; for(var i in config){ template = template.replace(new RegExp("{" + i + "}", "g"), config[i]); } //this is a pig's http://xxx, this http://xxx is beautiful 4. 匹配到的 var template = "this is word"; template = template.replace(/(word)/g, "a $1"); //this is a word 5. 更复杂点的就用replace function var template = "this is word"; template = template.replace(/\s(word)/g, function(word, p1){ //参数 //1. word:整个正则匹配到的 " word" 2. p1:(word) return " a " + p1 + "," + word; //" a word" }); //this is a word, word
相关文章推荐
- js中replace的用法(两种常用举例,还有好多用法不一一列举)
- js中replace的用法(两种常用举例,还有好多用法不一一列举)
- 常用笔记: JS实现VBS当中的Replace
- prototype.js常用函数及其用法
- prototype.js常用函数及其用法
- js中常用日期控件WdatePicker介绍和用法详解
- js中split和replace的用法实例
- JS Replace() 高级用法
- prototype.js常用函数及其用法
- Js中String对象方法replace()用法详解
- JS基于正则表达式的替换操作(replace)用法示例
- js中replace的用法
- prototype.js常用函数及其用法
- js中replace的用法
- js中replace的用法
- 【转载】Js中replace()的用法
- 本文实例讲述了js中正则的查找match()与替换replace()的用法。
- js一些用法及几个常用的ASP代码
- js中replace的用法
- 基础的prototype.js常用函数及其用法