---
title: "js-去掉空格"
author: "Panda也编程丶"
date: "2014-08-06T17:15:35Z"
tags: ["JS", "REPLACE"]
categories: ["编程语言"]
slug: "js"
draft: false
lastmod: "2014-08-06T17:15:35Z"
id: 2101034
url: "https://710542316.iteye.com/blog/2101034"
---

网上查阅的，几个测试了且通过了的js里面去掉空格的方法：

1、str = str.replace(/\[ ]/g,"")    //去掉所有空格

2、str = str.replace(/\\s+/g,"")    //去掉所有空格

3、str = str.replace(/^\\s+|\\s+$/g,"");    //去掉两头的空格

4、str = str.replace( /^\\s\*/, '');     //去掉左空格 

5、str = str.replace(/(\\s\*$)/g, "");    //去掉右空格