30

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
  • 556
  • 1
  • 6
  • 27
user3566643
  • 515
  • 1
  • 4
  • 8

1 Answers1

69

Use this:

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

Instead of this:

str.trim()
Cute_Ninja
  • 4,535
  • 4
  • 34
  • 59