JavaScript 扩展方法 trim 去除指定字符串

JavaScript中用于去除字符串中的空白字符的扩展方法【参考自:http://www.cnblogs.com/clyde/archive/2011/01/15/1936378.html

 1 String.prototype.trim = function(pos)
2 {
3 var trimRegex;
4 if (!pos)
5 {
6 pos = "g"; //
7 }
8 switch (pos.toLowerCase())
9 {
10 case "l":
11 trimRegex = /^\s*/g;
12 break;
13 case "m":
14 trimRegex = /\b\s*/g; //有汉字时无效
15 break;
16 case "r":
17 trimRegex = /\s*$/g;
18 break;
19 case "b":
20 trimRegex = /(^\s*)|(\s*$)/g;
21 break;
22 case "g":
23 trimRegex = /\s/g;
24 break;
25 default: //默认去除全部的空白字符
26 trimRegex = /\s/g;
27 break;
28 }
29 return this.replace(trimRegex, "");
30 }

 

测试文本 [ Tested string ... ]
模式 左 中 右 两端 全部
结果  

 

转载于:https://www.cnblogs.com/redleaf-me/archive/2011/12/19/js-trim.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值