1、正则去空格

a.去掉字符串中所有空格

"   hello world   ".replace(/\s+/g,"");//helloworld

b.去掉字符串左边空格

var str = "   hello world   ".replace(/^\s*/g,"");//hello world..

c.去掉字符串右边空格

var str = "   hello world   ".replace(/\s*$/g,"");//...hello world

d.去掉字符串左边和右边空格

var str = "   hello world   ".replace(/(^\s*)|(\s*$)/g,"");//hello world

可以给String的原型添加方法

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,''); }

然后使用

"   hello world".LTrim();
//hello world
"hello world ".RTrim();
//hello world
" hello world ".Trim();
//hello world

javascript 字符串去空格的更多相关文章

  1. 【SQL】字符串去空格解决方法

    一.表中字符串带空格的原因 1,空格就是空格. 2,控制符 显示为 空格. 二.解决方法 第一种情况,去空格的处理的比较简单,Replace(column,' ','') 就可以解决. 第二种情况,解 ...

  2. Foundation框架的一些实用方法:替换字符串,去空格,反转

    //定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组 NSMutableString *str = [NSMutableString stringWithForma ...

  3. ORACLE对字符串去空格处理(trim)

    首先便是这Trim函数.Trim 函数具有删除任意指定字符的功能,而去除字符串首尾空格则是trim函数被使用频率最高的一种.语法Trim ( string ) ,参数string:string类型,指 ...

  4. JavaScript字符串去除空格

    /*字符串去除空格*/ String.prototype.Trim = function() { return this.replace(/(^\s*)|(\s*$)/g, "") ...

  5. 字符串去空格 java , js和Jquery 方法

    1.  java方式 String.trim(); 2.js方式 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, "&qu ...

  6. javascript消除字符串两边空格的两种方式,面向对象和函数式编程。python oop在调用时候的优点

    主要是javascript中消除字符串空格,比较两种方式的不同 //面向对象,消除字符串两边空格 String.prototype.trim = function() { return this.re ...

  7. JavaScript去空格之trim()

    <script> var str=" ab cd "; alert("["+str.trim()+"]"); </scri ...

  8. String字符串操作--切割,截取,替换,查找,比较,去空格.....

    字符串拼接 直接用+号:String a = "I"; String b = "love"; String c = "you";String ...

  9. js使用正则表达式去空格

    写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...

随机推荐

  1. Dubbo 实例

    POM: <!-- Dubbo --> <dependency> <groupId>com.alibaba</groupId> <artifact ...

  2. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  3. Extjs定义的Fckeditor控件

    Ext.namespace('CRM.Panels'); //Ext.BoxComponent 这里继承是参考的Ext.form.Field CRM.Panels.Fckeditor = Ext.ex ...

  4. Git Step by Step – (8) Git的merge和rebase

    前面一篇文章中提到了"git pull"等价于"git fetch"加上"git merge",然后还提到了pull命令支持rebase模式 ...

  5. 【代码审计】iZhanCMS_v2.1 前台存储型XSS漏洞分析

      0x00 环境准备 iZhanCMS官网:http://www.izhancms.com 网站源码版本:爱站CMS(zend6.0) V2.1 程序源码下载:http://www.izhancms ...

  6. CPU特性漏洞测试(Meltdown and Spectre)

    2018年1月4日,国外安全研究人员披露了名为"Meltdown"和"Spectre"两组CPU特性漏洞,该漏洞波及到近20年的Intel, AMD, Qual ...

  7. Illegal mix of collations for operation 'concat'

    在t_employee表中,练习使用concat函数连接字符串时, mysql> select concat('工号为:',fnumber,'的员工的幸福指数:',fsalary/(fage-2 ...

  8. 转:git windows中文目录乱码问题解决

    Git的Windows版本Msysgit对中文的支持不够好 当使用时,会出现以下三种情况的中文乱码: 1.ls不能显示中文目录 解决办法:在git/git-completion.bash中增加一行:  ...

  9. numpy基本方法

    在学习python的时候常常需要numpy这个库,每次都是用一个查一个,这个,终于见到一个完整的总结了http://blog.csdn.net/blog_empire/article/details/ ...

  10. 【大数据系列】HDFS安全模式

    一.什么是安全模式 安全模式时HDFS所处的一种特殊状态,在这种状态下,文件系统只接受读数据请求,而不接受删除.修改等变更请求.在NameNode主节点启动时,HDFS首先进入安全模式,DataNod ...