0

I am trying to detect spaces on textarea my code is following

textarea.value.replace(/\s/g, ' ');

but it detects all whitespaces and I want to detect only spaces...

is any solution??

Max Leps
  • 469
  • 3
  • 12

3 Answers3

6
textarea.value.replace(/ /g, ' ');
// that's a space ------^
maerics
  • 151,642
  • 46
  • 269
  • 291
3

use a literal space, like so:

textarea.value.replace(/ /g, ' ');
Ray Waldin
  • 3,217
  • 1
  • 16
  • 14
0

Use this:

textarea.value.replace(/[ ]/g, ' ');
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46