32

How can I remove all the white space from a given string.

Say:

var str = "  abc de fog   ";
str.trim(); 

Gives abc de fog AND NOT abcdefog, which I want.

How can I get all the white space removed using JavaScript?

Prateekro
  • 566
  • 1
  • 6
  • 28
user3566643
  • 535
  • 1
  • 4
  • 8

1 Answers1

75

Use this:

str.replace(/\s+/g, '');

Instead of this:

str.trim()
Cute_Ninja
  • 4,742
  • 4
  • 39
  • 63