js 字符串格式化
2024-08-28 11:32:53
在js 文件中写一个函数
String.prototype.format = function(args) {
var result = this;
if (arguments.length > 0) {
if (arguments.length == 1 && typeof (args) == "object") {
for (var key in args) {
if(args[key]!=undefined){
var reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
}
}
else {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] != undefined) {
var reg= new RegExp("({)" + i + "(})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
}
return result;
}
s='我的名字是{0},家是{1}'.format('于洋','济南');
console.log(s);
测试,OK
取富文本编辑框的文本内容
2
最新文章
- python MethodType方法详解和使用
- 攻城狮在路上(伍)How tomcat works(四)Tomcat的默认连接器
- Volley(五)—— 自定义Request
- RM报表里的变量
- 服务器的Arch Linux,CentOS的,Debian的,Fedora的,Gentoo的,openSUSE的,Slackware的,和Ubuntu哪个好
- poj 3660 Cow Contest(传递闭包 Floyd)
- Quartz动态配置表达的方法
- IOS开发之KVC与KVO简述
- Html 中select标签的边框与右侧倒三角的去除
- Tree( 树) 组件[2]
- [T]各种字符串Hash函数比较
- MySQL数据库中日期中包涵零值的问题
- 分享:苹果APP更新上架被拒的另一种理由(Safety - Objectionable Content)
- c语言贪吃蛇详解1.画出地图
- centos7.2中文乱码解决办法
- 详解EBS接口开发之供应商导入(补充)--错误信息处理
- Ansible-基础
- [Swift]LeetCode74. 搜索二维矩阵 | Search a 2D Matrix
- js和jQuery的互相转换
- PAT甲题题解-1062. Talent and Virtue (25)-排序水题