最近仍在IE6徘徊,低版本的浏览器没有实现JavaScript 的trim() 和 format(). . 主要是这两个使用的比较多,先整理出来:

1、trim() -- 去除字符串中开始和结尾部分,所包含的指定字符。 默认是 空格; 参考: http://www.nowamagic.net/javascript/js_TrimInJavascript.php

 //prototype: trim
String.prototype.trim = function(strToRemove){
var reg = null;
// to trim space character, when not passing the param
if(strToRemove == null || strToRemove == undefined || strToRemove.length == 0){
reg = /(^\s*)|(\s*$)/g;
}
else{
reg = new RegExp("(^"+strToRemove+"*)|("+strToRemove+"*$)", "g");
}
return this.replace(reg, "");
}
//test: "abc,".trim(",") --- > abc

2、format() -- 格式化字符串(两种方式实现), 参考: http://www.cnblogs.com/nonlyli/archive/2008/08/14/1267480.html

A. 基于'实例'的方法,放在原型链上

 //prototype: format
String.prototype.format = function() {
// need a variable to store the params to replace, or it will be overrided in the MatchedFunction
var args = arguments;
return this.replace(/{(\d+)}/g, function(m, i){ return args[i];});
}
//test: "abc{0}".format(123) --- > abc123

B. 基于'类'的方法,静态方法。 这个在原来的基础上做了更改,因为已经使用for循环了,再使用正则替换,感觉有点浪费了; 而且,这个方法和上一个只是,参数的位置不一致,向后移一位即可。。。

 //static: format
String.format = function() {
var args = arguments;
if(args.length == 0){
return "Argument null";
}
var source_str = args[0];
return source_str.replace(/{(\d+)}/gm,
//attention:here, sub_index is String,need to convert to int when adding
function(item_matched, sub_index){
return args[parseInt(sub_index)+1];
}
);
}
//test: String.format("abc{1}", "123", 999) -- > abc999

这个format的静态方法,还有一个利用Slice的更巧妙的实现, 参考: http://witmax.cn/js-function-string-format.html

 //another ingenious method to replace, using the slice method of Array,
// reference: http://witmax.cn/js-function-string-format.html
String.format = function(source_str){
var param_args = Array.prototype.slice.call(arguments, 1);
return source_str.replace(/{(\d+)}/gm,
function(item_matched, sub_index){
return param_args[sub_index];
}
);
}

JavaScript 学习笔记 -- String.trim + format的更多相关文章

  1. Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  2. Java程序猿JavaScript学习笔记(2——复制和继承财产)

    计划和完成在这个例子中,音符的以下序列: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaSc ...

  3. Java程序猿JavaScript学习笔记(14——扩大jQuery UI)

    计划和完成这个例子中,音符的顺序如下: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScr ...

  4. javascript学习笔记(四) Number 数字类型

    数字格式化方法toFixed().toExponential().toPrecision(),三个方法都四舍五入 toFixed() 方法指定小数位个数  toExponential() 方法 用科学 ...

  5. JavaScript学习笔记之数组(二)

    JavaScript学习笔记之数组(二) 1.['1','2','3'].map(parseInt) 输出什么,为什么? ['1','2','3'].map(parseInt)//[1,NaN,NaN ...

  6. JavaScript学习笔记[0]

    JavaScript学习笔记[0] 使用的是廖雪峰JavaScript教程. 数据类型 Number 表示数字,不区分浮点整形. === 比较时不转化数据类型. == 反之. NaN与任何值都不想等, ...

  7. Java程序猿的JavaScript学习笔记(10—— jQuery-在“类”层面扩展)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

  8. JavaScript:学习笔记(2)——基本概念与数据类型

    JavaScript:学习笔记(2)——基本概念与数据类型 语法 1.区分大小写.Test 和 test 是完全不同的两个变量. 2.语句最好以分号结束,也就是说不以分号结束也可以. 变量 1.JS的 ...

  9. Java程序猿的JavaScript学习笔记(6——面向对象模拟)

    计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...

随机推荐

  1. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  2. exists的用法

    今天突然看到之前自己写的一个代码 久久没想通为毛.. 看来笔记还是需要 exists可以代替子查询in  比in 更高效   默认是exists 查询中包含有数据则条件成立..否则没数据 select ...

  3. GitHub和SourceTree入门教程

    -->本教程适用于主流的开源网站github和bitbucket,个人认为sourceTree还是比较好用的git客户端,支持windows和mac os. -->soureceTree的 ...

  4. 降低磁盘IO使Oracle性能优化(转)

    文章转自:http://blog.chinaunix.net/uid-26813519-id-3207996.html 硬件方面虽然只占Oracle性能优化的一个方面(另一方面是软件),但是仍不可忽视 ...

  5. BZOJ2005 莫比乌斯反演

    题意:http://www.lydsy.com/JudgeOnline/problem.php?id=2005 实际上把这些被挡住的点的坐标和能量值列举出来可以发现有个公式: “对于坐标系第一象限任意 ...

  6. QTVA-2015-198545、WooYun-2015-104148 .NET Framework Arbitrary File Permissions Modify Vul

    catalog . Description . Effected Scope . Exploit Analysis . Principle Of Vulnerability . Patch Fix 1 ...

  7. C#关闭窗口代码

    if (MessageBox.Show("请您确认是否退出(Y/N)", "系统提示", MessageBoxButtons.YesNo, MessageBox ...

  8. PHP守护进程

    php也是可以直接进行守护进程的启动与终止的,相对于shell来说会简单很多,理解更方便,当然了php的守护进程要实现自动重启还是要依赖于shell的crontab日程表,每隔一段时间去执行一次脚本看 ...

  9. CentOS安装MongoDB

    1. touch /etc/yum.repos.d/mongodb.repo vi /etc/yum.repos.d/mongodb.repo [mongodb]        name=MongoD ...

  10. CF 204B Little Elephant and Cards

    题目链接: 传送门 Little Elephant and Cards time limit per test:2 second     memory limit per test:256 megab ...