// 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, "");
}alert(" dddsdsd ".Trim())
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, "");
}alert(" dddsdsd ".Trim())
function strtrim(strText)
{
if(strText!=null)
{ while(strText.substring(0,1)==" ")
{
strText=strText.substring(1,strText.length);
}
while(strText.substring(strText.length-1,strText.length)==" ")
{
strText=strText.substring(0,strText.length-1);
}
}
return strText;
}
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
还是这个最好了
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, "");}
var str = " abc de ";
alert(str.Trim());
</script>
Javascript中为String对象添加trim
<script language=Javascript> //自己动手为string添加Trim
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, "");}
alert(" meizz ".Trim());
</script>
仔细看与楼上的写法不同哟.
function String.prototype.Alltrim(){return this.replace(/ /g, "");}
var abc=" 12 3 4 ";
abc.Alltrim();
alert(abc);//显示为1234
var str = " abc de ";
window.execScript("str=Trim(str)",'Vbscript');
alert(str);
</script>