jQuery String Template Format Function
来源:互联网 发布:linux 常用命令 chm 编辑:程序博客网 时间:2024/05/22 14:06
https://www.sitepoint.com/jquery-string-template-format-function/
Pretty useful jQuery function I’ve called “formatVarString”. Its takes a string as the first argument with n arguments after with which to perform variable substitution (returning variables as part of a string using parenthesis).
You can simply use {1}, {2}, {3} etc to reference variables in a string.
Usage
formatVarString('we love {1}.', 'jQuery4u');//output: "we love jQuery4u."formatVarString('{1} is a {2} aimed to help you learn {3}.', 'jQuery4u', 'blog', 'jQuery');//output: "jQuery4u is a blog aimed to help you learn jQuery."The jQuery Format Function
源码:
var JQUERY4U = {};JQUERY4U.UTIL = {formatVarString: function() { var args = [].slice.call(arguments); if(this.toString() != '[object Object]') { args.unshift(this.toString()); } var pattern = new RegExp('{([1-' + args.length + '])}','g'); return String(args[0]).replace(pattern, function(match, index) { return args[index]; }); }}
使用:
JQUERY4U.UTIL.formatVarString('{1} is a {2} aimed to help you learn {3}.', 'jQuery4u', 'blog', 'jQuery');//output: "jQuery4u is a blog aimed to help you learn jQuery."
Was this helpful?
More:
javascript return string variables {1}
阅读全文
0 0
- jQuery String Template Format Function
- function template
- function template
- Format Function
- A high performance string format function for JavaScript
- Just another high performance string format function for JavaScript
- Call to a member function format() on string
- template string
- template string
- String.Format
- string.format()
- string format
- string.Format
- String.format()
- String.Format
- String format
- string.Format
- string.Format
- 面试题1_赋值运算符的实现
- 007-认知不协调|改变他人态度的秘诀
- UVa 1587
- Leetcode : 23. Merge k Sorted Lists
- Linux必知必会-理解内存使用统计命令free
- jQuery String Template Format Function
- 磁盘IO:缓存IO、直接IO、内存映射
- 在Mac中安装JDK1.7失败
- mbr,gpt,uefi,secureboot装机实战
- 六月英语
- Java垃圾回收开销降低的5条建议
- 基本选择器
- 小问题: caffe2 安装到自己目录下
- 异或 The Great XOR