最近仍在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-1014】火星人prefix Splay + 二分 + Hash

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 5852  Solved: 1871[Submit] ...

  2. 【bzoj2242】 SDOI2011—计算器

    http://www.lydsy.com/JudgeOnline/problem.php?id=2242 (题目链接) 题意 给出y,z,p.求:1.yz mod p:2.xy=z(mod p):3. ...

  3. win7下firefox浏览器不能使用

    win7下firefox浏览器不能使用,只有360浏览器才能使用. 使用360安全卫士,到更多工具里面选择"LSP修复",就可以了. 原来是因为安装了土豆加速,然后卸载导致的. 看 ...

  4. shell命令bc

    简介 bc支持浮点数的精度运算(Bash不支持浮点数运算) 运行方式 一.CLI 二.PIPE 示例 一.浮点数运算 变量scale:设置小数点后面的位数  # 默认scale=0 echo &quo ...

  5. BackGroundWorker控件的使用注意

    该控件有三个事件: DoWork .ProgressChanged 和 RunWorkerCompleted 在程序中调用RunWorkerAsync方法则会启动DoWork事件的事件处理,当在事件处 ...

  6. ubantu eclipe

    sudo tar zxvf '/tmp/eclipse-inst-linux64.tar.gz' -C/usr/lib 4.在终端输入: $ sudo gedit /usr/share/applica ...

  7. LaTeX test

    \begin{equation} x^2+1=2 \end{equation} \begin{equation} x^2+y=3 \end{equation} $\dfrac{2\pi}{N}$

  8. 安装PhantomJS

    安装步骤 # 安装依赖软件 yum -y install wget fontconfig # 下载PhantomJS wget -P /tmp/ https://bitbucket.org/ariya ...

  9. POJ 2796 Feel Good(单调栈)

    传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investig ...

  10. response压缩响应

    思路:1.通过filter向目标页面传递一个自定义的response对象 2..在这个response对象中通过重写getOutputStream方法和getWriter方法使目标资源调用 该方法输出 ...