代码
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, ""); } // 去掉右空格
例
<script type="text/javascript">
function trim(str){ //删除左右两端的空格
return str.replace(/(^s*)|(s*$)/g, "");
}
function ltrim(str){ //删除左边的空格
return str.replace(/(^s*)/g,"");
}
function rtrim(str){ //删除右边的空格
return str.replace(/(s*$)/g,"");
}
</script>
去除所有空格
<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.Trim = function()
{
return this.replace(/(^s*)|(s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(s*$)/g, "");
}
//-->
</SCRIPT>
下面来我们来看看Js脚本中"/s表示什么"
s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ fnrtv]。
请紧记是小写的s