JavaScript 小工具
1. 字符串格式化輸出
Orders of {1} or more {0}'
{0},{1}代表第幾個參數,包含了完善的異常處理。當給定參數少于格式化串中占位符個數時,未找到的直接留白。
// 格式化字符串 // 包含了異常處理 // formatStr('Orders of {1} or more {0}', 'aaa') // output: Orders of {1} or more aaa function formatStr(template, ...args) { if (typeof template !== 'string') { return template } if (args.length === 0) { return template } // 通過正則替換%s return template.replace(/(\{\d+\})/g, function(match, offset, string) { var indexStr = match.slice(1, -1) if (indexStr) { var index = parseInt(indexStr) if (index < args.length) { return args[parseInt(index)] } } return match }) }
??請樓主喝杯豆漿??
![]() |

浙公網安備 33010602011771號