Remove whitespaces inside a string in javascript
I've read this question about javascript trim, with a regex answer.
Then I expect trim to remove the inner space between Hello and World.
function myFunction() {
alert("Hello World ".trim());
}
EDITED
Why I expected that!?
Nonsense! Obviously trim doesn't remove inner spaces!, only leading and trailing ones, that's how trim works, then this was a very wrong question, my apologies.
For space-character removal use
"hello world".replace(/s/g, "");
for all white space use the suggestion by Rocket in the comments below!
Probably because you forgot to implement the solution in the accepted answer. That's the code that makes trim()
work.
update
This answer only applies to older browsers. Newer browsers apparently support trim()
natively.
上一篇: jQuery复选框.prop()问题