Inside this article we will see the concept i.e Remove All Whitespace From a String in JavaScript Tutorial. Article contains the classified information about removing all spaces from a string value using JavaScript.
If you are looking for a solution i.e How to remove spaces from a string using JavaScript then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.
In Javascript there are several different methods available by which you can remove whitespaces from string value. We will see those methods in this tutorial.
Learn More –
- Laravel 9 to_route() and str() New Helper Function Tutorial
- How To Check if an Object is Empty in JavaScript Tutorial
- Laravel 9 YajraBox Datatable Custom Filter and Search Tutorial
- Laravel 9 How To Add Conditional Class in Blade Template
Let’s get started.
Example #1: Remove Whitespaces Using ‘replace’ & regex
Here,
We will use the concept of Javascript i.e Remove All Whitespace From a String.
const str = ' h e l l o '; const noWhitespace = str.replace(/\s/g, ''); console.log(noWhitespace); // 'hello'
Console Output
Example #2: Remove Whitespaces Using ‘replaceAll’ & regex
Here,
We will use the concept of Javascript i.e Remove All Whitespace From a String.
const str = ' h e l l o '; const noWhitespace = str.replaceAll(/\s/g, ''); console.log(noWhitespace); // 'hello'
Console Output
Example #3: Remove Whitespaces Using ‘replaceAll’
Here,
We will use the concept of Javascript i.e Remove All Whitespace From a String.
const str = ' h e l l o '; const noWhitespace = str.replaceAll(' ', ''); console.log(noWhitespace); // 'hello'
Console Output
Example #4: Remove Whitespaces Using ‘split’ & ‘join’
Here,
We will use the concept of Javascript i.e Remove All Whitespace From a String.
const str = ' h e l l o '; const noWhitespace = str.split(/\s+/).join(''); console.log(noWhitespace); // 'hello'
Console Output
We hope this article helped you to learn Remove All Whitespace From a String in JavaScript Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.