Warning: session_start(): open(/tmp/sess_3fc5a712820a280cf36bb6ac30095499, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Replace Multiple Spaces With A Single Space In JavaScript - LearnShareIT

How To Replace Multiple Spaces With A Single Space In JavaScript

Replace Multiple Spaces with a Single Space in JavaScript

This article will help you replace multiple spaces with a single space in JavaScript by using replace() function, split(), and join() methods combined with regular expression. Let’s take a look at the methods below in detail.

Replace multiple spaces with a single space in JavaScript

Using replace() method and RegEx 

In JavaScript, you must be familiar with the replace() function. It is used to replace the string of characters in the original string with another string you want. 

And to replace multiple spaces into a single space in a string, we also use the replace() function, with the first parameter being a regular expression representing the spaces in the string and the second argument just a space. It will replace many of the original spaces in the string.

Example:

let str = '     Learn     Share     IT  ';
console.log('Original string: '+ str);
 
let newStr1 = str.replace(/ +/g, ' ');
console.log('String after replacing multiple spaces: '+ newStr1);
 
let newStr2 = str.replace(/\s+/g, ' ');
console.log('String after replacing multiple spaces: '+ newStr2);
 
let newStr3 = str.replace(/ {2,}/g, ' ');
console.log('String after replacing multiple spaces: '+ newStr3);

Output

Original string:      Learn     Share     IT  
String after replacing multiple spaces:  Learn Share IT 
String after replacing multiple spaces:  Learn Share IT 
String after replacing multiple spaces:  Learn Share IT 

In the above example, you can see that we used 3 different regular expressions, but they all show multiple spaces in the search string. These are the regular expressions I’ve used and what they mean. There are many more regular expressions in Javascript that you can learn about in detail here.

  • + : Check character appears one or more times.
  • \s : Find characters that are spaces.
  • {2,} : Check the character appears at least 2 times.
  • g : Compares the entire search string.

Using split() and join() method

The second way to replace multiple spaces into one space is using the split() method to split the original string into an array of ['Learn', 'Share', 'IT']. Then use the join() function to join the elements together. The parameter in the join() function will be a space, which is the distance between the elements.

Example:

let str = '     Learn     Share     IT  ';
console.log('Original string: '+ str);
 
let newStr4 = str.split(/ +/g).join(' ');
console.log('String after replacing multiple spaces: '+ newStr4);
 
let newStr5 = str.split(/\s+/g).join(' ');
console.log('String after replacing multiple spaces: '+ newStr5);
 
let newStr6 = str.split(/ {2,}/g).join(' ');
console.log('String after replacing multiple spaces: '+ newStr6);

Output

Original string:      Learn     Share     IT  
String after replacing multiple spaces:  Learn Share IT 
String after replacing multiple spaces:  Learn Share IT 
String after replacing multiple spaces:  Learn Share IT 

Summary

So, you already know how to replace multiple spaces with a single space in JavaScript by using replace() function and split() and join() methods combined with regular expression. Many regular expressions help us find a lot of gaps, but you need to remember a simple method that is easy to apply or know how to build those expressions. Good luck with your practice. See you in the next post.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *