惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
H
Help Net Security
Jina AI
Jina AI
V
V2EX
G
Google Developers Blog
B
Blog
GbyAI
GbyAI
U
Unit 42
爱范儿
爱范儿
腾讯CDC
Engineering at Meta
Engineering at Meta
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
小众软件
小众软件
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
博客园 - 聂微东
The Cloudflare Blog
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏

博客园 - 空空儿

硬盘分区、寻址和系统启动过程 asp.net异步页 获取SQL Server数据行的物理地址信息(%%lockress%% & %%physloc%%) 利用Spire.DataExport将数据(Database/DataTable)导成各种文件 使用XML的value()方法将多行数据合并成一列 获取所有的外键信息 ASP.NET application and page life cycle SQL Server 2005 CLR 调用Web Service需要注意的几个问题 SQL SERVRE 2005 CLR TVF错误:从用户定义的表值函数获取新行时出错:Data access is not allowed in this context. PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 在C#里使用属性 - 空空儿 - 博客园 Read text file (txt, csv, log, tab, fixed length) 批量写数据---将XML数据批量写入数据库 对对象类型和调用方法属性进行存储以提升反射性能 过程 sp_addlinkedsrvlogin,第 91 行解密过程中出错的解决办法 生成大量随机字符串不同实现方式的效率对比 用SQL SERVER 2005新提供的命令实现行列转换 APM (异步编程模型) 利用宏让ERStudio生成代码文件
JavaScript里String.Format方法的实现
空空儿 · 2011-06-26 · via 博客园 - 空空儿

在Javascript里通过原型扩展和正则表达式实现类似于C#里的String.Format方法.

方法实现:

String.prototype.format = function (args) {
       
var str = this;
       
return str.replace(new RegExp("{-?[0-9]+}""g"), function(item) {
       
var intVal = parseInt(item.substring(1, item.length - 1));
       
var replace;
       
if (intVal >= 0) {
            replace 
= args[intVal];
       } 
else if (intVal === -1) {
            replace 
= "{";
       } 
else if (intVal === -2) {
            replace 
= "}";
       } 
else {
            replace 
= "";
       }
       
return replace;
    });
};

 使用如下:

var str = "She {1} {0}{2} by the {0}{3}. {-1}^_^{-2}";
str 
= str.format(["sea""sells""shells""shore"]);
alert(str);
//out put: She sells seashells by the seashore. {^_^}

原文地址:String.Format in JavaScript