日期:2014-05-16 浏览次数:20490 次
<script type="text/javascript">
/**
*新增String物件的3個Method
**/
//去除前後(左右)空白
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, "");
}
function _Trim()
{
document.getElementById("ans").value = document.getElementById("str").value.Trim();
}
function _LTrim()
{
document.getElementById("ans").value = document.getElementById("str").value.LTrim();
}
function _RTrim()
{
document.getElementById("ans").value = document.getElementById("str").value.RTrim();
}
</script>
<div style="float: left;">
<div>
輸入要去空白字串
<input id="str" type="text">
</div>
<div>
輸出除去結果字串
<input id="ans" type="text">
</div>
<div>
<input onclick="_Trim(); return false;" type="button" value="去除左右空白">
<input onclick="_LTrim(); return false;" type="button" value="去左邊空白">
<input onclick="RTrim(); return false;" type="button" value="去右邊空白">
</div>
</div>