简单的:str = jQuery.trim(str);

 var temp = " aa b   ";
console.log("cc" + temp);
temp = jQuery.trim(temp);
console.log("cc" + temp);

自己写的:

 1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
5 <title></title>
6 <script src="Js/jquery-1.8.2.min.js"></script>
7 <script type="text/javascript">
8 function trim(str) {
9 str = str.replace(/^(\s|\u00A0)+/, '');
10 for (var i = str.length - 1; i >= 0; i--) {
11 if (/\S/.test(str.charAt(i))) {
12 str = str.substring(0, i + 1);
13 break;
14 }
15 }
16 return str;
17 }
18 $(function () {
19 $("#bt").click(function () {
20 var temp = $('#tes');
21 var c = temp[0];
22 alert('test' + trim(temp.val()) + 'str')
23 })
24 })
25 </script>
26 </head>
27 <body>
28 输入:<input id="tes" type="text" /><br />
29 <input id="bt" type="button" value="检测" />
30 </body>
31 </html>

js去除首尾空格的更多相关文章

  1. JS去掉首尾空格 简单方法大全(原生正则jquery)

    JS去掉首尾空格 简单方法大全 var osfipin= ' http://www.cnblogs.com/osfipin/ '; //去除首尾空格 osfipin.replace(/(^\s*)|( ...

  2. bash 中 trim 字符串(去除首尾空格) - grep 去空行

    在 bash 下如何去除一个字符串首尾的空格(也就是 trim)呢?其实有一个简单的办法: $ echo $STR 注 意 $STR 不要带引号.因为 $STR 展开后,会作为 echo 的参数.那么 ...

  3. js去除字符串空格(空白符)

    使用js去除字符串内所带有空格,有以下三种方法: ( 1 ) replace正则匹配方法 去除字符串内所有的空格:str = str.replace(/\s*/g,""); 去除字 ...

  4. js去除字符串空格

    str.replace(/\s+/g,""); str.replace(/\s|\xA0/g,""); empName=empName.replace(/^\s ...

  5. 请用js去除字符串空格?

    方法一:使用replace正则匹配的方法 去除所有空格: str = str.replace(/\s*/g,""); 去除两头空格: str = str.replace(/^\s* ...

  6. JS 去除字符串空格

    $.trim()是jQuery提供的函数,用于去掉字符串首尾的空白字符. "abc 123 def".replace(/\s/g, "") 去除所有的空格

  7. js去除中间空格

    / 功能: 1)去除字符串前后所有空格 // 2)去除字符串中所有空格(包括中间空格,需要设置第2个参数为:g) function Trim(str,is_global) { var result;  ...

  8. js去除左右空格

     replace方法去掉字符串的空格 //去左空格; s=s.replace(/(^\s*)/g, ""); //去右空格; s= s.replace(/(\s*$)/g, &qu ...

  9. js 去除左右空格

    /*****************************************************Method1*************************************** ...

随机推荐

  1. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  2. 男装电子零售商East Dane即将面世_衣装_YOKA时尚网

    男装电子零售商East Dane即将面世_衣装_YOKA时尚网 男装电子零售商East Dane即将面世

  3. CodeForces 160D - Distance in Tree 树型DP

    题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...

  4. 编译ycm库

    在安装完YCM之后,重新打开vim还会出现如下的报错信息:ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; ...

  5. SQL Server索引进阶:第十级,索引内部结构

    原文地址: Stairway to SQL Server Indexes: Level 10,Index Internal Structure 本文是SQL Server索引进阶系列(Stairway ...

  6. HDU 1695 GCD(欧拉函数+容斥原理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:x位于区间[a, b],y位于区间[c, d],求满足GCD(x, y) = k的(x, ...

  7. [NOIP2001提高组]CODEVS1014 Car的旅行路线(最短路)

    最短路,这个不难想,但是要为它加边就有点麻烦..还好写完就过了(虽然WA了一次,因为我调试用的输出没删了..),不然实在是觉得挺难调的.. ------------------------------ ...

  8. [置顶] 老孟 DB2 V9.7 ESE(一)产品部署 基于centOS 6.4

    本文安装系统CENTOS 6.4 DB2位数64 安装中涉及目录位置各位可自行定义 生产系统为安全和性能考虑,一般将DB2实例目录.日志目录.归档日志目录.表空间目录区分开,可建立/db2home / ...

  9. php mysql实现栏目分类递归

    header("content-type:text/html;charset=utf-8"); $dbhost = "localhost";   // 数据库主 ...

  10. [LeetCode]题解(python):085-Maximal Rectangle

    题目来源: https://leetcode.com/problems/maximal-rectangle/ 题意分析: 给定一个二维的二进制矩阵,也就是只包括0 和 1的,找出只包括1的最大的矩阵的 ...