2

I'm trying to get rid of spaces in a string, but it seems not to be working. This doesn't return the string without spaces:

"{ color: 'blue',".replace(" ",'') and "{ color: 'blue',".trim()

I've verified that those are spaces (char code 32), and I've even done this:

var x = "{ color: 'blue'," x.replace(x[1],'')

FYI, doing this in chrome's console. Am I going crazy?

B T
  • 57,525
  • 34
  • 189
  • 207
  • `.trim()` only removes leading and trailing whitespace. – Felix Kling Sep 14 '17 at 21:24
  • I always find it weird that people mods mark questions as duplicate on SO even when the question/confusion is very different. Duplicate answers isn't the same thing as a duplicate question. – B T Sep 14 '17 at 21:32
  • How is your confusion different than the one in https://stackoverflow.com/questions/6623231/remove-all-white-spaces-from-text ? – Felix Kling Sep 14 '17 at 21:33
  • Because I didn't even realize it was removing *any* spaces. I just thought I was trapped in a waking nightmare – B T Sep 14 '17 at 21:47
  • And you think this minor variance warrants a complete new question? Are the duplicates not useful to you? But ultimately it's not possible to satisfy everybody. I rather close more often than not often enough. Luckily I don't have the last word. If the community feels strongly about it they have ways to reopen the question. – Felix Kling Sep 14 '17 at 21:49
  • This question is not what I feel strongly about. I'm just expressing that I wish SO had a way to distinguish "this question has answers that answer this question" from "duplicate question" markings. Its misleading in a lot of cases, and I know for a fact it causes a lot of strife in the community - especially with newbs. For what its worth, I think closing too often vs not often enough is kind of like innocent before proven guilty. Would you rather jail too many people than not enough? No. No you wouldn't. – B T Sep 15 '17 at 21:38

2 Answers2

5

It does remove space, but not all of them just the first one, to remove all you should do

let str = "{   color: 'blue',".replace(/ /g,'');

console.log(str);
marvel308
  • 10,288
  • 1
  • 21
  • 32
2

Try something like this:

x= x.replace(/\s/g, '');
Jack Fay
  • 21
  • 1