- 論壇徽章:
- 0
|
今天用到javascript去掉一個文本框中字符串兩端的空格,開始還以為有trim,ltrim,rtrim函數(shù)(js總中的trim() 參數(shù)不能取到值,超郁悶。。。。),最后找到用正則實現(xiàn)這樣功能的自定義函數(shù)。
function trim(str){ //刪除左右兩端的空格
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str){ //刪除左邊的空格
return str.replace(/(^\s*)/g,"");
}
function rtrim(str){ //刪除右邊的空格
return str.replace(/(\s*$)/g,"");
}
使用方法
function aaa()
{
var s=document.getElementById("文本框ID").value;
alert(trim(s));
}
哈哈,搞定了,祝賀下。!
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/88320/showart_1720297.html |
|