var desired = stringToReplace.replace(/[^\w\s]/gi, '')

As was mentioned in the comments it's easier to do this as a whitelist - replace the characters which aren't in your safelist.

The caret (^) character is the negation of the set [...], gi say global and case-insensitive (the latter is a bit redundant but I wanted to mention it) and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s).

Answer from annakata on Stack Overflow
Discussions

regex - How to remove special characters from a string using Javascript - Stack Overflow
Could someone please help me to remove special characters from a string using javascript or Jquery. Note: I would like to remove only a specific set of special characters but not replacing it wit... 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
Get rid of special characters in string
Not a bubble question - a javastring question. Have some problems getting .replace to work when removing special characters from a string of special characters. For most special characters, just using the special character works. For others like $ or ^, using the special character doesnโ€™t work. More on forum.bubble.io
๐ŸŒ forum.bubble.io
13
0
July 1, 2023
how to replace all accented characters with English equivalents
Not AFAIK within a single regex. You can simplify that a bit in some regex engines by using a character collation class, searching for [[=a=]] and replacing it with a. However, the common way is to do unicode normalization first to NFKD (decomposing combined characters into their parts) and then remove the diacritics. Several Python examples here More on reddit.com
๐ŸŒ r/regex
10
4
April 18, 2023
๐ŸŒ
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 - The transliteration map converts accented characters to their ASCII equivalents before the regex strips anything remaining. 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(/^\.+/, ''
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ javascript-regex-how-to-replace-special-characters
JavaScript regex - How to replace special characters?
The replace() method with regex patterns provides a powerful way to remove or replace special characters in JavaScript strings.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ javascript โ€บ javascript remove special characters
How to Remove Special Characters in JavaScript | Delft Stack
February 2, 2024 - Here, the RegExp object matches the text within the pattern. You can find more about it here. Suppose we get a string from the HTML element, replace special characters, and display it back on your screen. Letโ€™s practice that with jQuery. ... <!DOCTYPE html> <html> <head> <title> Remove Special Characters</title> </head> <body> <h2>The Original String</h2> <p id="stringValue">Do [a search-for special characters] in string &:search and, "remove"#@ them:</p> <h2>String After Replacement</h2> <p id="demo"></p> </body> </html>
๐ŸŒ
Coding Beauty
codingbeautydev.com โ€บ home โ€บ posts โ€บ how to remove special characters from a string in javascript
How to Remove Special Characters From a String in JavaScript
October 13, 2022 - To remove all special characters from a string, call the replace() method on the string, passing a whitelisting regex and an empty string as arguments, i.e., str.replace(/^a-zA-Z0-9 ]/g, '').
๐ŸŒ
Maker's Aid
makersaid.com โ€บ home โ€บ remove special characters from a string in javascript
Remove Special Characters From a String in JavaScript - Maker's Aid
April 2, 2023 - To remove special characters from a string in JavaScript, we will use the String.replace() method with a global RegEx rule that looks for all matches of the characters we want removed, then replaces them with empty quotes ('').
Find elsewhere
๐ŸŒ
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!
๐ŸŒ
Metring
ricardometring.com โ€บ javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
The only addition, in this case, was to create 2 groups in the regex through ([ group 1 ]|[ group 2 ]) and add to group 2 the regular expression [^0-9a-zA-Z], which means: anything that's not (^) 0-9, a-z or A-Z, is also replaced.
๐ŸŒ
Tocinocode
tocinocode.com โ€บ blog โ€บ regular-expression-remove-special-characters-js
Tocinocode - Blog - Regular expression remove special characters js
The replace() function is used to replace matching characters with an empty string, which is equivalent to removing them. In the example, the original text string is "Hello! Tocinocode?" and the regular expression removes the special characters, leaving the clean text string that is displayed ...
๐ŸŒ
Codegive
codegive.com โ€บ blog โ€บ js_regex_remove_special_characters.php
js regex remove special characters: Unlock Flawless Data Cleaning & Input Security
April 6, 2026 - To remove special characters from a string using JavaScript regex, you typically use the replace() method with a regular expression that matches non-alphanumeric characters, such as /[^a-zA-Z0-9 ]/g, and replace them with an empty string.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ articles on trending technologies โ€บ write a regular expression to remove all special characters from a javascript string?
Write a Regular Expression to remove all special characters from a JavaScript String?
June 23, 2020 - Learn how to write a regular expression to remove all special characters from a JavaScript string effectively. This guide provides step-by-step instructions and examples.
๐ŸŒ
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.
๐ŸŒ
CodePen
codepen.io โ€บ MoyArt โ€บ pen โ€บ gjKLEj
JS - REGEX to remove spaces and special characters
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
๐ŸŒ
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. If you wish to include the backslash \ or closing square bracket ] you must precede them with a backslash (the first backslash tells the system to read the next character but not interpret it. See Example 2 below). ... Using onChange will replace the characters as soon as the user clicks away from current field, or hits Save. Using onKeyup will replace the characters as soon as they are typed. 1. To remove all characters that aren't part of the basic Latin alphabet and 0-9 (such as .
๐ŸŒ
CodexWorld
codexworld.com โ€บ home โ€บ how to guides โ€บ how to trim a specific character from the start and end of a string using javascript?
How to Trim a Specific Character from the Start and End of a String using JavaScript? - CodexWorld
April 14, 2021 - Regular Expression (RegEx) is the easiest way to trim a specific character from a string in JavaScript. Use the JavaScript replace() method with RegEx to remove a specific character from the string.
๐ŸŒ
Bubble
forum.bubble.io โ€บ need help
Get rid of special characters in string - Need help - Bubble Forum
July 1, 2023 - Not a bubble question - a javastring question. Have some problems getting .replace to work when removing special characters from a string of special characters. For most special characters, just using the special charaโ€ฆ
๐ŸŒ
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.