代码
复制代码 代码如下:
function String.prototype.Trim() { return this.replace(/(^/s*)|(/s*$)/g, ""); } // 去掉左右空格
function String.prototype.Ltrim() { return this.replace(/(^/s*)/g, ""); } // 去掉左空格
function String.prototype.Rtrim() { return this.replace(/(/s*$)/g, ""); } // 去掉右空格
例
去除所有空格
JS 去字符串空格 总结
str为要去除空格的字符串:
去除所有空格:
str = str.replace(/s+/g,"");
去除两头空格:
str = str.replace(/^s+|s+$/g,"");
去除左空格:
str=str.replace( /^s*/, '');
去除右空格:
str=str.replace(/(s*$)/g, "");
复制代码 代码如下:
去除所有空格:
str = str.replace(/s+/g,"");
去除两头空格:
str = str.replace(/^s+|s+$/g,"");
去除空格(TimeSheet用过)
arg0=arg0.replace(/s+$|^s+/g,"");
下面来我们来看看Js脚本中"/s表示什么"
s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ fnrtv]。
请紧记是小写的s,另外细心的朋友可能看到有时候s+有时候是*号,大家可以看下面的解释
| 代码/语法 | 说明 |
|---|---|
| * | 重复零次或更多次 |
| + | 重复一次或更多次 |
| ? | 重复零次或一次 |
| {n} | 重复n次 |
| {n,} | 重复n次或更多次 |
| {n,m} | 重复n到m次 |



