标题 | javascript string对象格式化的方法 |
内容 | javascript string对象格式化,下面是参考代码: /** * 字符串格式化,类似于java中的MessageFormat.format() 函数 * e.g. * 'This is a {0} not a {1}.'.format('string', 'num') => This is a string not a num. * 'This is a {0}, not a {1}. so parse to {2} first.'.format('string', 'num', 'num') => This is a string not a num. so parse to num first. * 'This is a {0}, not a {1}. so parse to {1} first.'.format('string', 'num') => This is a string not a num. so parse to num first. * 'This is a {0}, not a {1}. so parse to {2} first.'.format('string', 'num') => This is a string not a num. so parse to {2} first. */ String.prototype.format = function() { var args = arguments; return this.replace(/\{(\d+)\}/g, function(){ var val = args[arguments[1]]; alert(val + ' ' + arguments[0]); return (! val) ? arguments[0] : val; }); }; |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。