When handling strings in JavaScript, there may be instances where it’s necessary to eliminate special symbols, leaving spaces intact within a string. To achieve this, we utilize regular expressions alongside the String.prototype.replace() method.
In this article, we will explore the process of removing all special symbols except spaces from a string in JavaScript.
Contents Overview:
- Eradicating Special Symbols except Space Using Regex
- Eradicating Special Symbols except Space Using match()
- Summary
- Frequently Asked Questions
Eradicating Special Symbols except Space Using Regex
Regular Expressions (Regex) in JavaScript are employed for searching, finding, and substituting strings that adhere to specific patterns. Regex can also be applied to eradicate special symbols from a string while preserving spaces. The replace() function is utilized to convert all special symbols into empty strings.
Sample Code:
function removeSpecialCharacters(str) {
return str.replace(/[^a-zA-Z0-9 ]/g, '');
}
let inputString = "Welc#$%^&ome to Int&*()ellipa@@@at";
let result = removeSpecialCharacters(inputString);
console.log(result);
Result:
Welcome to Intellipaat

Clarification:
- str.replace(/[^a-zA-Z0-9 ]/g, ”): This regex identifies any character that isn’t a lowercase letter (a – z), uppercase letter (A – Z), digit (0 – 9), or space.
- The g flag ensures that all special symbols are substituted with an empty string.
Eradicating Special Symbols except Space Using match()
Another approach to eliminate special symbols from a string involves the match() function and concatenating the filtered characters to yield the result.
Sample Code:
function removeSpecialCharactersUsingMatch(str)
let inputString = "Intelli@#$%paat is awesom$%^&*()e!";
let result = removeSpecialCharactersUsingMatch(inputString);
console.log(result);
Result:
Intellipaat is awesome

Clarification:
- match(/[a-zA-Z0-9 ]/g): This function filters all valid characters (letters, numbers, and spaces).
- || []: This segment ensures that we don’t receive null if no matches occur.
- .join(”): This method concatenates the filtered characters into a new string.
Summary
Throughout this article, we have discussed methods to eliminate all special symbols except spaces from a string using regular expressions in JavaScript. We employed the replace() function and match() for these operations. For those interested in enhancing their web development knowledge, consider exploring Intellipaat’s Web Development Course to become an expert in web development.
Frequently Asked Questions
The article Eradicate all special symbols except spaces from a string utilizing JavaScript was originally featured on Intellipaat Blog.