Javascript-如何删除字符串开头的空白 [英] Javascript - How to remove the white space at the start of the string

查看:98
本文介绍了Javascript-如何删除字符串开头的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除字符串开头的空白 它应该只删除字符串开头的空格,其他空格应该在那里.

I want to remove the white space which is there in the start of the string It should remove only the space at the start of the string, other spaces should be there.

var string=' This is test';

推荐答案

这就是您想要的:

function ltrim(str) {
  if(!str) return str;
  return str.replace(/^\s+/g, '');
}

也适用于IE8 +中的常规修饰:

Also for ordinary trim in IE8+:

function trimStr(str) {
  if(!str) return str;
  return str.replace(/^\s+|\s+$/g, '');
}

并修剪右侧:

function rtrim(str) {
  if(!str) return str;
  return str.replace(/\s+$/g, '');
}

这篇关于Javascript-如何删除字符串开头的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆