You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:

const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Petar Ivanov on Stack Overflow
🌐
CoreUI
coreui.io › answers › how-to-remove-special-characters-from-a-string-in-javascript
How to remove special characters from a string in JavaScript · CoreUI
May 20, 2026 - For more on string replacement, see how to replace all occurrences of a string in JavaScript. File systems have strict rules about allowed characters. Sanitizing filenames prevents errors and security issues when saving user-provided names. // Remove characters not allowed in most file systems const sanitizeFilename = (filename) => { return filename .replace(/[<>:"/\\|?*\x00-\x1F]/g, '') // forbidden chars .replace(/\s+/g, '_') // spaces to underscores .replace(/^\.+/, '') // no leading dots (hidden files) .slice(0, 255) // max filename length } console.log(sanitizeFilename('my<file>:name?.txt
Discussions

Removing special characters from a string
Currently working on making a palindrome function. I have the gist of it. Just can’t get this last part. I’ve tried so many examples from so many stockoverflow posts and this still doesn’t work for me. Can someone help me figure out what’s wrong? var str = "abc's test#s"; ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
13
0
July 30, 2017
javascript - Regular expression - replace special characters, except dot - Stack Overflow
Reread the question, they want to replace special characters except dot, so hyphen has to be removed. More on stackoverflow.com
🌐 stackoverflow.com
javascript - Regex remove all special characters except numbers? - Stack Overflow
3 How to remove all non numeric characters (excluding minus, dot and comma) in a string in Javascript? 4 Remove all the characters and special characters except underscores, dashes and numbers More on stackoverflow.com
🌐 stackoverflow.com
Removing Special Characters Using an Built In Function | OutSystems
Removing Special Characters Using an Built In Function More on outsystems.com
🌐 outsystems.com
January 27, 2022
🌐
Abacadvor
sfa.abacadvor.site › javascript-remove-special-characters-from-string-except-dot.html
Javascript remove special characters from string except dot. Remove all special characters except space from a string using JavaScript
One thing to note is that if the value ends with a period dotor even any whitespace, then it will end with a hyphen. You could also try to globally replace any non-alphanumeric character and white space by using the function. Learn more. Remove spacesdots and special chars from a string and replace with hyphen in jQuery Ask Question.
🌐
RSWP Themes
rswpthemes.com › home › javascript tutorial › how to remove spaces and special characters from a string in javascript
How to Remove Spaces and Special Characters from a String in JavaScript
June 3, 2025 - The regex method str.replace(/[^a-zA-Z0-9]/g, '') is the most flexible and efficient for removing special characters while keeping alphanumeric ones. Regex is often the fastest for complex patterns.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript remove special characters
How to Remove Special Characters in JavaScript | Delft Stack
February 2, 2024 - This tutorial teaches about how to remove special characters from a string using JavaScript with and without jQuery.
🌐
freeCodeCamp
forum.freecodecamp.org › programming
Removing special characters from a string - Programming - The freeCodeCamp Forum
July 30, 2017 - Currently working on making a palindrome function. I have the gist of it. Just can’t get this last part. I’ve tried so many examples from so many stockoverflow posts and this still doesn’t work for me. Can someone help me figure out what’s wrong? var str = "abc's test#s"; str.replace(/[&\/\\#,+()$~%.'":*? {}]/g,'_'); console.log(str);
🌐
TutorialsPoint
tutorialspoint.com › article › javascript-regex-how-to-replace-special-characters
JavaScript regex - How to replace special characters?
The JavaScript built-in method string.replace() can be used to replace a portion of a supplied string with another string or a regular expression.
Find elsewhere
🌐
sebhastian
sebhastian.com › javascript-remove-special-characters-from-string
JavaScript Remove Special Characters From a String | sebhastian
October 25, 2023 - To remove special characters from a JavaScript string, you need to use the replace() method and define a regular expression to search for the special characters that need to be removed.
🌐
SmartWiki
wiki.smartsimple.com › wiki › Removing_Special_Characters
Removing Special Characters - SmartWiki
Any individual character can be specified within the square brackets except double quote " which has special meaning.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › replace-special-characters-in-a-string-with-underscore-_-in-javascript
Replace special characters in a string with underscore (_) in JavaScript - GeeksforGeeks
July 11, 2025 - // Requiring the lodash library const _ = require("lodash"); // Original array let string = _.replace('Stay# In', /[&\/\\#, +()$~%.'":*?<>{}]/g, '_'); // Using the _.replace() method let replace_elem = _.replace(string); // Printing the output console.log(replace_elem);
🌐
Code Beautify
codebeautify.org › blog › remove-special-sharacters-from-string-javascript
Remove Special Characters From String Javascript
February 22, 2024 - Approach 1 : 1 2 3 4 5 6 7 8 9 10 11 function removeSpecialCharacters(inputString) { // Use a regular expression to match and replace special characters return inputString.replace(/[^\w\s]/gi, ''); } // Example usage: const originalString = "Hello!
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-a-character-from-string-in-javascript
Remove a Character From String in JavaScript - GeeksforGeeks
February 25, 2026 - One of the most common methods to remove a character from a string is by using the replace() method. This method searches for a specified character or pattern in a string and replaces it with something else, which can be an empty string ("") ...
🌐
Stack Overflow
stackoverflow.com › questions › 79897841 › remove-special-characters-from-a-string-except-emojis
javascript - Remove special characters from a string except emojis - Stack Overflow
Emojis are Unicode characters, so they don’t match that range and get removed. If you're using modern JavaScript (ES2018+), you can use Unicode property escapes to keep letters, numbers, spaces, and emojis: stringWithEmoji.replace( /[^\p{L}\p{N}\s\p{Emoji}\u200D]/gu, '' );
🌐
Intellipaat
intellipaat.com › home › blog › remove all special characters except space from a string using javascript
Remove all special characters except space from a string using JavaScript - Intellipaat
February 3, 2026 - So far in this article, we have learned how we can remove all special characters except space from a string using regular expressions in JavaScript. We have to use replace() function and match() to do this task.