<script language='javascript'>
var str = ' 01 2 3 12 ';
alert(":" + str + ":");
//去掉左边空格,包括tab键和换行符
String.prototype.ltrim=function()
{
return this.replace(/(^\s*)/g,'');
}
alert(":" + str.ltrim() + ":");
//去掉右边空格,包括tab键和换行符
String.prototype.rtrim=function()
{
return this.replace(/(\s*$)/g,'');
}
alert(":" + str.rtrim() + ":");
//去掉左右边空格,包括tab键和换行符
String.prototype.lrtrim=function()
{
return this.replace(/(^\s*)|(\s*$)/g,'');
}
alert(":" + str.lrtrim() + ":");
//去掉全部空格,包括tab键和换行符
String.prototype.trimAll=function()
{
return this.replace(/(\s*)/g,'');
}
alert(":" + str.trimAll() + ":");
</script>
javascript trim 去除 空格
最新推荐文章于 2024-06-21 14:31:26 发布