<html>
<head>
</head>
<body >
<Input type = "button" value = "button" onclick = "test()"/>
<body>
</html>
<script type = "text/javascript">
String.prototype.trim = function()
{
var reExtraSpace = /^\s*(.*?)\s+$/;
return this.replace(reExtraSpace,"$1");
}
function test(){
var textStr = " hello workd ";
alert("(" + textStr.trim() + ")");
//alert("asfasdfdsa");
}
</script>
注:上述代码中加粗显示($1)部分为正则表达式的反向引用,参见另一篇文章正则表达式的反向引用。