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
  • 439
  • 3
  • 12

3 Answers3

6
textarea.value.replace(/ /g, ' ');
// that's a space ------^
maerics
  • 143,080
  • 41
  • 260
  • 285
3

use a literal space, like so:

textarea.value.replace(/ /g, ' ');
Ray Waldin
  • 3,137
  • 15
  • 14
0

Use this:

textarea.value.replace(/[ ]/g, ' ');
Patrick Kostjens
  • 4,925
  • 6
  • 27
  • 45