Table of Contents
When working with JavaScript to make the web page and application interactive, strings are the main part of the language that is used in most projects. Being an integral part of JavaScript programming, strings need to be placed well to avoid errors. Getting extra spaces in a string is common, especially when you are beginners who are just starting with the language. You need to remove all the spaces from a string in JavaScript to get the project completed.
When you have a string with spaces and you try to display it in the browser, you may not get to see the spaces. So, you need to remove extra spaces. Let’s shed a light on how to remove all spaces from a string
How to remove all the spaces from a string in JavaScript
We use a few ways to remove extra spaces from a string.
Using replaceAll Method
When you call this method, pass a string that contains a space as the first parameter and the second as an empty string str.replaceAll(' ', '')
. In return, you will get a new string with replaced matches. Take look at the code:
const example_str1 = 'Remove All Spaces From A String In JavaScript';
const replaced_str1 = example_str1.replaceAll(' ', '');
console.log(replaced_str1);
Output:
RemoveAllSpacesFromAStringInJavaScript
With this method, we match all the spaces in a string. The replacement string helps when placed as an empty string. The original string is never changed by the ‘replaceAll’ method instead its returns a new string. It is also noted that in JavaScript, strings are immutable.
Using the ‘replace’ Method for Internet Explorer
If you are using Internet Explorer, you need to know that the ‘replaceAll’ method discussed above is not supported by this browser. You should use the ‘replace’ method instead of the ‘replaceAll’ method. To remove all spaces from a string in JavaScript, you need to call this method and pass a regular expression that can match the spaces as the first parameter and the second as an empty string. It returns a new string without spaces. Note: This method supported in IE 6-11
const example_str2 = 'Remove All Spaces From A String Using The Replace Method For Internet Explorer';
const replaced_str2 = example_str2.replace(/ /g, '');
console.log(replaced_str2);
Output:
RemoveAllSpacesFromAStringUsingTheReplaceMethodForInternetExplorer
Here, the ‘g’ (global) flag is used to match all occurrences of the spaces and not only the first one. Apart from that, we have discussed above that the replace method never changes the original string. You will get a new string in return with one or two replaced matches.
Using replace Method with Regex
In the case, the regular expression can’t match the tabs and new lines, you need to remove all the spaces, new lines, and tabs. Check out the example
const example_str3 = 'Remove All Spaces From A String Using Replace Method With Regex';
const replaced_str3 = example_str3.replace(/\s/g, '');
console.log(replaced_str3);
Output:
RemoveAllSpacesFromAStringUsingReplaceMethodWithRegex
Being a special character, the ‘\s
’ is used in the regex to match spaces, new lines, and tabs.
Conclusion
We have discussed the ways to remove all spaces from a string in JavaScript. All the methods are quite simple to use. I hope you find it useful!
Leave a comment