1
String.prototype.Trim = function()
2
{
3
return this.replace(/(^\s*)|(\s*$)/g, "");
4
}
5
String.prototype.LTrim = function()
6
{
7
return this.replace(/(^\s*)/g, "");
8
}
9
String.prototype.Rtrim = function()
10
{
11
return this.replace(/(\s*$)/g, "");
12
}
使用的實(shí)例:

2

3

4

5

6

7

8

9

10

11

12

1
<script language=javascript>
2
String.prototype.Trim = function()
3
{
4
return this.replace(/(^\s*)|(\s*$)/g, "");
5
}
6
var s = " leading and trailing spaces ";
7
window.alert(s + " (" + s.length + ")");
8
s = s.Trim();
9
window.alert(s + " (" + s.length + ")");
10
</script>

2

3

4

5

6

7

8

9

10
