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. iframe设置高度为100%

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)

    一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...

  3. jquery 复制文本到剪切板插件(非 flash)

    原创插件,转载请声明出处!!! jquery.copy.js 内容如下: /*! * jQuery Copy Plugin * version: 1.0.0-2018.01.23 * Requires ...

  4. Cookie利用神器:CookieHacker

    转自evilcos的博客 看到那么多苦逼的跨站师在问Cookie利用工具,不忍心,还是把自己写的Chrome扩展开源出来吧,功能极简,仿造<我的渗透利器>里提到的Original Cook ...

  5. Ansible 远程执行脚本

    1. 先在服务端创建一个 shell 脚本 [root@localhost ~]$ cat /tmp/test.sh #!/bin/bash echo "hello world" ...

  6. /etc/issue

    /etc/issue 与 /etc/motd 作用一致,都是用于显示欢迎信息,区别在于 /etc/issue 是在 login 提示符之前显示,而 /etc/motd 则在在用户成功登录系统之后显示 ...

  7. 【译】Kafka最佳实践 / Kafka Best Practices

    本文来自于DataWorks Summit/Hadoop Summit上的<Apache Kafka最佳实践>分享,里面给出了很多关于Kafka的使用心得,非常值得一看,今推荐给大家. 硬 ...

  8. 使用es6的蹦床函数解决递归造成的堆栈溢出

      首先,我们先定义一个函数,使用递归的思想写求和的方法: function sum(x, y) { if (y > 0) { return sum(x + 1, y - 1); } else ...

  9. nginx+php-fpm 报“File not found.”

    找网上找了很多帖子,大都是说nginx中的$document_root$换成绝对路径,但是依然不能解决问题 后再把php-fpm配置文件中的 [www]下边的 usr = apache group = ...

  10. 老树开新花:DLL劫持漏洞新玩法

    本文原创作者:丝绸之路 <img src="http://image.3001.net/images/20150921/14428044502635.jpg!small" t ...