/*删除两侧空格*/
function trim(ui){
var notValid=/(^\s)|(\s$)/;
while(notValid.test(ui)){
ui=ui.replace(notValid,"");
}
return ui;
}
/*删除前导空格*/
function leftTrim(ui){
var notValid=/^\s/;
while(notValid.test(ui)){
ui=ui.replace(notValid,"");
}
return ui;
}
/*删除后置空格*/
function rightTrim(ui){
var notValid=/\s$/;
while(notValid.test(ui)){
ui=ui.replace(notValid,"");
}
return ui;
}
/*删除所有空格*/
function allTrim(ui){
var notValid=/\s/;
while(notValid.test(ui)){
ui=ui.replace(notValid,"");
}
return ui;
}
/*是否包含前导空格*/
function isLeftSpace(ui) {
var valid=/^\s/;
return (valid.test(ui));
}
/*是否包含后置空格*/
function isRightSpace(ui) {
var valid=/\s$/;
return (valid.test(ui));
}
/*是否任何一侧包含空格*/
function isBothSpace(ui) {
var valid=/(^\s)|(\s$)/;
return (valid.test(ui));
}
/*是否包含空格*/
function isIncSpace(ui) {
var valid=/\s/;
return (valid.test(ui));
}
------------------------------------------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
<!--
//出处:网上搜集
// Trim() , Ltrim() , RTrim()
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, "");
}
String.prototype.AllTrim = function()
{
var str = document.getElementById('space').value;
var notValid=/\s/;
while(notValid.test(str)){
str=str.replace(notValid,"");
}
return str;
}
//-->
</SCRIPT>
<input type="text" value=" 前 后 都 是 空 格 " id="space">
<input type="button" value="去前后空格" οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.Trim();document.getElementById('space').select();">
<input type="button" value="去前空格" οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.LTrim();document.getElementById('space').select();">
<input type="button" value="去后空格" οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.RTrim();document.getElementById('space').select();">
<input type="button" value="去所有空格" οnclick="javascript:document.getElementById('space').value=document.getElementById('space').value.AllTrim();document.getElementById('space').select();">
<input type="button" value="还原" οnclick="javascript:document.getElementById('space').value=' 前 后 都 是 空 格 ';">
<a href="http://www.yaoasnsi.com" target="_blank">访问yaosansi.com</a>