JS字符串格式化~欢迎来搂~~
/*
函数:格式化字符串
参数:str:字符串模板; data:数据
调用方式:formatString("api/values/{id}/{name}",{id:101,name:"test"});
formatString("api/values/{0}/{1}",101,"test");
****字符串中需要替换的数据用 {} 括起来,注意花括号里面不能有空格,data建议使用自定义对象,也就是python中的字典
*/
function formatString(str, data) {
if (!str || data == undefined) {
return str;
} if (typeof data === "object") {
for (var key in data) {
if (data.hasOwnProperty(key)) {
str = str.replace(new RegExp("\{" + key + "\}", "g"), data[key]);
}
}
} else {
var args = arguments,
reg = new RegExp("\{([0-" + (args.length - 1) + "])\}", "g");
return str.replace(reg, function(match, index) {
return args[index - (-1)];
});
}
return str;
}
例子来了~
text_str = "<tr>
<th>{id}</th>
<th>{name}</th>
<th>{author}</th>
<th>{publish}</th>
</tr>"
data = {"id":1,"name":"金瓶","author":"小强","publish":"西北出版社"};
new_str = formatString(str, data) ## new_str ===> "<tr>
<th>1</th>
<th>'金瓶'</th>
<th>"小强"</th>
<th>"西北出版社"</th>
</tr>"
JS字符串格式化~欢迎来搂~~的更多相关文章
- js字符串格式化扩展方法
平时使用js的时候会遇到很多需要拼接字符串的时候,如果是遇到双引号和单引号混合使用,经常会搞混.在C#中有string.Format方法,使用起来非常方便,也很容易理解,所以找到一种参考C#的form ...
- JS字符串格式化
//字符串格式化String.prototype.format = function () { var values = arguments; return this.replace(/\{(\d+) ...
- JS字符串格式化函数 string.format
原生JS写的仿C#的字符串format函数,在此基础上又增加了便于JS使用的字面量对象参数. 参照C#中的规则,调用的时候会检测字符串格式,如果字符串格式不规范,或者传入的参数为null或undefi ...
- js 字符串格式化方法
String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...
- js 字符串格式化
在js 文件中写一个函数 String.prototype.format = function(args) { var result = this; if (arguments.length > ...
- [javascript]编码&i字符串格式化&nput历史记录&清空模态框
js中编码问题 https://www.haorooms.com/post/js_escape_encodeURIComponent 我在前端js添加时候创建dom时候,有汉字,发现是乱码就研究了下 ...
- JS 字符串转日期格式 日期格式化字符串
/** * @author 陈维斌 http://www.cnblogs.com/Orange-C/p/4042242.html%20 3 * 如果想将日期字符串格式化,需先将其转换为日期类型Date ...
- 使用js在HTML中自定义字符串格式化方法
Python中支持字符串格式化,其基本形式如下: str = "I'm {name},{age} years old" print(str.format(name="te ...
- 表单序列化json字符串和js时间格式化
js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = { ...
随机推荐
- BZOJ 2458: [BeiJing2011]最小三角形 (分治)
分治就是了. 类似于分治找最近/远点对. CODE #include <bits/stdc++.h> using namespace std; const double eps = 1e- ...
- UTF-8 无 BOM
[参考] UTF8最好不要带BOM,附许多经典评论 Visual Studio UTF-8 无 BOM 一站式解决办法https://blog.csdn.net/dolphin98629/articl ...
- pycharm mysql数据源配置、SQL方言配置
会发现有提示,看着不爽,但不影响运行程序, 这里提示没有配置数据源,现在配置MYSQL数据源 然后看到右边Database选项卡,点击 然后可能会出现网络防火墙提示,选择全部允许,之后可能会在pych ...
- Win7 : 'java' is not recognized as internal or external command,
Java application is not working in Win 7 64-bit http://answers.microsoft.com/en-us/windows/forum/win ...
- LA 6979 Known Notation 构造+贪心 铜牌题
题意:给出一个字符串,有两种操作: 1.插入一个数字 2.交换两个字符 问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*) #include <cstdio> #inclu ...
- codeforces#1187E. Tree Painting(树换根)
题目链接: http://codeforces.com/contest/1187/problem/E 题意: 给出一颗树,找到一个根节点,使所有节点的子节点数之和最大 数据范围: $2 \le n \ ...
- 0ctf-ezdoor-复现分析
在学习opcache的时候,看到了这个题目,刚好有环境,就来复现一下,这个题目也让我学到了很多. 创建镜像: docker build -t 0ctf-ezdoor . 启动容器: docker ru ...
- DS博客作业04--树大作业
1.树的存储结构 本组采用的树的存储结构为链式结构,选择如图所示的结构体 Name为结点的名称 LevelNum为孩子节点的个数 *Children[20]用来指向不同的孩子结点(类似于二叉树的结构体 ...
- JS基础_call和apply
call()和apply() - 这两个方法都是函数对象的方法,需要通过函数对象来调用 - 当对函数调用call()和apply()都会调用函数执行 - 在调用call和apply可以将一个对象指定为 ...
- Centos镜像下载地址
https://blog.csdn.net/weixin_42430824/article/details/81019039