1. Remove the left space.replace (/^\s*/g,"");
2 Remove the space on the right.replace (/\s*$/g,"");
3 Remove the spaces before and after.replace(/(^\s*)|(\s*$)/g,"")
4 Remove all spaces.replace (/\s+/g,"")
Blank test: <input type="text" id="test" name="text" onchange="blank();" ><br>
<script>
$(function(){
var test =$('#test').val();
alert(test);
})
function blank(){var test =$('#test').val();
<%--remove the space on the left --%>
alert("a"+test);
test = test.replace(/^\s*/g,"");
alert("Remove the left space"+test);
<%--Remove the space on the right--%>
alert(test+"a");
test = test.replace(/\s*$/g,"");
alert(test+"Remove the space on the right");
<%--Remove spaces before and after the string--%>
test = test.replace(/(^\s*)|(\s*$)/g, "");
alert("Remove the spaces before and after the string"+test+"a");
<%--Remove all spaces in the string--%>
test = test.replace(/\s+/g,"");
alert("a"+test+"a");
}
</script>