/*
函数:格式化字符串
参数: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字符串格式化~欢迎来搂~~的更多相关文章

  1. js字符串格式化扩展方法

    平时使用js的时候会遇到很多需要拼接字符串的时候,如果是遇到双引号和单引号混合使用,经常会搞混.在C#中有string.Format方法,使用起来非常方便,也很容易理解,所以找到一种参考C#的form ...

  2. JS字符串格式化

    //字符串格式化String.prototype.format = function () { var values = arguments; return this.replace(/\{(\d+) ...

  3. JS字符串格式化函数 string.format

    原生JS写的仿C#的字符串format函数,在此基础上又增加了便于JS使用的字面量对象参数. 参照C#中的规则,调用的时候会检测字符串格式,如果字符串格式不规范,或者传入的参数为null或undefi ...

  4. js 字符串格式化方法

    String.prototype.format = function(args) { var result = this; if (arguments.length > 0) { if (arg ...

  5. js 字符串格式化

    在js 文件中写一个函数 String.prototype.format = function(args) { var result = this; if (arguments.length > ...

  6. [javascript]编码&i字符串格式化&nput历史记录&清空模态框

    js中编码问题 https://www.haorooms.com/post/js_escape_encodeURIComponent 我在前端js添加时候创建dom时候,有汉字,发现是乱码就研究了下 ...

  7. JS 字符串转日期格式 日期格式化字符串

    /** * @author 陈维斌 http://www.cnblogs.com/Orange-C/p/4042242.html%20 3 * 如果想将日期字符串格式化,需先将其转换为日期类型Date ...

  8. 使用js在HTML中自定义字符串格式化方法

    Python中支持字符串格式化,其基本形式如下: str = "I'm {name},{age} years old" print(str.format(name="te ...

  9. 表单序列化json字符串和js时间格式化

    js时间格式化 new Date().format("时间格式") Date.prototype.format = function(fmt) { var o = {        ...

随机推荐

  1. springboot 详解RestControllerAdvice(ControllerAdvice)(转)

    springboot 详解RestControllerAdvice(ControllerAdvice)拦截异常并统一处理简介 @Target({ElementType.TYPE}) @Retentio ...

  2. 脚本实现PXE装机

    #!/bin/bash read -p "请输入您的装机服务器:" ip read -p "请输入您想要的ip最小值(1-255):" min read -p ...

  3. 【转】别人写的pe代码

    // PEOperate.cpp: implementation of the PEOperate class. // //////////////////////////////////////// ...

  4. Codevs 1105 过河 2005年NOIP全国联赛提高组

    1105 过河 2005年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在河上有一座独木桥,一只青蛙 ...

  5. 五一培训 清北学堂 DAY5

    今天是吴耀轩老师的讲解- 今天的主要内容:图论 如何学好图论? 学好图论的基础:必须意识到图论! 图 邻接矩阵存图: 其缺点是显而易见的:1. 空间复杂度O(n^2)不能接受:2.有重边的时候很麻烦: ...

  6. Postman集合/文件夹/请求中脚本的执行顺序

    Postman的Collection(集合)/Folder(集合的子文件夹)/Request(请求)中都有Pre-request Script(请求前脚本)和Tests(请求后脚本) 这个功能类似于不 ...

  7. 用python实现的简易记牌器的demo

    实现功能很简单: 初始时 1到10 以及 jkq各 4张,大小王 共两张 只要输入相应的牌号:1到10,例如 >>1     J.K.Q :例如>>j     >> ...

  8. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

  9. javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String 可长点心吧

    在网上搜了好多帖子都说<c:forEach items="${list }" var="stu">标签list没有加${}: 可我的问题不是这个,而 ...

  10. 使用django uwsgi 导致磁盘满

    lsof |grep delete |sort -nrk 7|more kill 掉这些进程