今天遇到了以关于JavaScript 中怎么去掉 字符串中前后两段的空格 ,我只好向就得js中也后Trim() 函数,后来试试了不
行,就网上找了下解决方法,其中用到了正则表达式 ,整理了下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>测试去空格-柯乐义</title>
<script type="text/javascript">
//http://www.cnblogs.com/roucheng/
//Trim() , Ltrim() , RTrim() 函数
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 testTrim()
{
var name;
name = document.keleyiform.uname.value.Trim();
//name = document.keleyiform.uname.value.RTrim();
//name = document.keleyiform.uname.value.LTrim();
if (name.length == 0) {
alert("用户明不能为空!" + name.length);
document.myform.