If you want to remove all whitespace from a string and not just the literal space character, use \s instead. You can remove the whitespace in a string by using a string.replace(/\s/g, “”) regular expression.
2 ways to remove whitespace from a string
- Regular expression
- Split() + Join()
Remove ALL white spaces from text
using replace() to repeat the regex:
Don’t Miss : Remove space between string in jQuery
Example 1: Using Split() with Join() method
const member_comments = ' HowDoYouDo Friends '; console.log(member_comments.split(' ').join(''));
Using Regular Expression
Example : 1
const member_comments = ' HowDoYouDo Friends '; console.log(member_comments.replace(/ /g,''));
Example : 2
const member_comments = ' HowDoYouDo Friends '; console.log(member_comments.replace(/\s/g,''));
var member_comments = " HowDoYouDo Friends! "; alert(member_comments.trim());
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.