String trim 刪除空格

<script language=”javascript”>
/**
* 刪除左右兩端的空格
*/
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,”);
}
</script>

運用示例如下:

<script type=”text/javascript”>
alert(document.getElementById(’abc’).value.trim());
alert(document.getElementById(’abc’).value.ltrim());
alert(document.getElementById(’abc’).value.rtrim());
</script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章