javascript trim whitespace – How to Trim String in JavaScript?

javascript trim whitespace – Remove whitespace from both sides of a string: JavaScript String trim(). You have to tell replace() to repeat the regex: str.trim() method is used to remove the white spaces from both the ends of the given string.

var website_str = '   WwwPakainfocom   ';

console.log(website_str.trim());

javascript trim whitespace

The whitespace characters are space, tab, no-break space, etc. The trim() is a built-in string function in JavaScript, which is used to trim a string.

Remove all white space from string JavaScript

Use this:

website_str.replace(/\s+/g, '');

//OR

website_str.trim()

Remove ALL white spaces from text

using replace()

.replace(/ /g,'')

In JavaScript, trim() is a string method that is used to remove whitespace characters from the start and end of a string.

use “replaceAll”:

console.log(' j k    l m n   o p   '.replaceAll(' ',''));

Don’t Miss : Javascript Remove All Spaces

I hope you get an idea about javascript trim whitespace.
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.

Leave a Comment