使用replace函数+正则表达式实现
str.replace(/(^\s*)|(\s*$)/g, "");
创建方法
<script language="javascript">
function String.prototype.trim(){
returnthis.replace(/(^\s*)|(\s*$)/g, "");
}
function String.prototype.ltrim(){
returnthis.replace(/(^\s*)/g, "");
}
function String.prototype.rtrim(){
returnthis.replace(/(\s*$)/g, "");
}
alert(str.trim());
</script>
str.replace(/(^\s*)|(\s*$)/g, "");
创建方法
<script language="javascript">
</script>