You can use replaceAll() function to replace underscores with spaces in a string using JavaScript. It will help you to filter input from the user. In this article I will show you how to do it with specific steps . Let’s go into detail.
Replace underscores with spaces in a string using JS
Use the replaceAll() method
ReplaceAll() method will help you to replace all strings that match the pattern with replacement. ReplaceAll() method will return a new string that was replaced.
Syntax:
str.replaceAll(pattern,replacement)
Parameters:
- pattern: this can be a string, a symbol, or a typical expression you want to replace.
- replacement: all the strings that match the pattern will be replaced by replacement.
Example:
const aString = "Hello_From_Learn_Share_IT";
const filtedString = aString.replaceAll("_", " ");
console.log(filtedString);
Output:
Hello From Learn Share IT
Here I replace all underscores with space. You can also provide whatever string follows your purpose. You can also replace:
Example:
const aString = "Hello0From0Learn0Share0IT";
const replacedString = aString.replaceAll("0", " ");
console.log(replacedString);
Output:
Hello From Learn Share IT
Here I replace all “0” strings with space and assign them to the replacedString variable. And origin string will turn into a string without “0” string.
Use replace() method
The replace() method will help you search for a string or value and then replace it with another. The replace() method will return a new string that was replaced. It is also like the replaceAll() method, but without replacing all strings that match, replace() method only replaces the first string that matches the pattern you give.
Example:
const aString = "Hello_From Learn Share IT";
const filtedString = aString.replace("", " ");
console.log(filtedString);
Output:
Hello_From Learn Share IT
Of course, if you have many duplicate strings, the replace() method won’t work with all of them.
Example:
const aString = "Hello_From_Learn_Share_IT";
const filtedString = aString.replace("_", " ");
console.log(filtedString);
Output:
Hello From_Learn_Share_IT
Here I only can replace the first underscore with replace() method. But you can replace all with method replace by using RegExp reference. RegExp helps you configure a particular target to which you want to apply the effect. Regexp is a feature shown in ECMAScript (ES1). You can read more about it here. Regexp reference has modifiers that help you apply the effect to global which is “g” stand for global. This modifier will help you apply to effect to all strings that match the condition.
Example:
const aString = "Hello_From_Learn_Share_IT";
const filtedString = aString.replace(/_/g, " ");
console.log(filtedString);
Output:
Hello From Learn Share IT
Here with “g” regexp, I indicate all underscore then I can replace all of them with space.
Summary
In this tutorial, I explained how to replace underscores with spaces in a string using JS. You can use replaceAll() method or replace() combine with regexp “g” if you want to replace all. Thank you for reading!
Maybe you are interested:
- How To Check If String Contains Whitespace In JavaScript
- Replace all Spaces in a String in JavaScript
- Remove file extension from a string using JavaScript

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm