博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 字符串去空格
阅读量:4322 次
发布时间:2019-06-06

本文共 725 字,大约阅读时间需要 2 分钟。

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

 

转载于:https://www.cnblogs.com/wjw-blog/p/7526429.html

你可能感兴趣的文章
android EditText自动弹出和自动关闭软键盘
查看>>
吉特日化MES-工业生产盲区
查看>>
Codeforces 517 #B
查看>>
实验四
查看>>
Scramble String
查看>>
php之接口概念
查看>>
01、计算机原理结构,及冯诺依曼体系结构
查看>>
Python 列表基本操作
查看>>
Linux TC基于CBQ队列的流量管理范例
查看>>
Python hashlib and hmac
查看>>
Fitnesse Page 简单使用
查看>>
C#.net 创建XML
查看>>
1057 数零壹
查看>>
隐马尔科夫模型(上)
查看>>
asp.net mvc FluentValidation 的使用
查看>>
java JvM
查看>>
HDU 1009 Just a Hook
查看>>
python基础之数据类型
查看>>
CSS居中初探
查看>>
element-ui table 点击分页table滚动到顶部
查看>>