How to remove white spaces from a string in JavaScript

How to remove white spaces from a string in JavaScript

In this post we will give you information about How to remove white spaces from a string in JavaScript. Hear we will give you detail about How to remove white spaces from a string in JavaScriptAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to remove the white spaces from a string using JavaScript.

Consider we have a string with a lot of white spaces.

const str = 'this  is  a  big  old  string';

We can use the replace() method to remove the white spaces from an entire string.

Example:

const str = 'this  is  a  big  old  string';str.replace(/s/gi , '')//"thisisabigoldstring"

In the above code, we have passed two arguments to replace() method the first one is regular expression /s/gi and the second one is empty string ”, so that the replace() will replace the all white-spaces with an empty string(no space).

Similarly, we can also use the following regular expression.

const str = 'this  is  a  big  old  string';str.replace(/W/gi , '')//"thisisabigoldstring"

Hope this code and post will helped you for implement How to remove white spaces from a string in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US