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
🌐
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 - // 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')) // Result: 'myfilename.txt' console.log(sanitizeFilename('...hidden file v2.pdf')) // Result: 'hidden_file___v2.pdf' // Preserve the extension while cleaning the name const sanitizeWithExtension = (filename) => { const lastDot = filename
Discussions

Javascript string replace with regex to strip off illegal characters - Stack Overflow
Need a function to strip off a set of illegal character in javascript: |&;$%@" ()+, This is a classic problem to be solved with regexes, which means now I have 2 problems. This is what... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Regex to remove specific special characters
How can I remove specific special characters from a string using regex in JavaScript? single quotes ' double quotes " ampersand & registered ® trademark ™ More on stackoverflow.com
🌐 stackoverflow.com
August 3, 2017
How can I remove a character from a string using JavaScript? - Stack Overflow
I am so close to getting this, but it just isn't right. All I would like to do is remove the character r from a string. The problem is, there is more than one instance of r in the string. However, ... More on stackoverflow.com
🌐 stackoverflow.com
October 29, 2014
Regex remove all special characters except numbers?
pythonjavascriptc#reactjsjavaa...gularnext.jsspring-bootmachine-learningsqlexceliosazuredocker ... Next You’ll be prompted to create an account to view your personalized homepage. Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I would like to remove all special ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnjavascript › how to remove ([]"") using javascript regex
r/learnjavascript on Reddit: How to Remove ([]"") using Javascript Regex
January 12, 2022 -

Hi all,

I'm passing a string that looks like this:

[("MT-1","MT-2","MT-3","MT-4")] 

I want to write a Javascript function which converts the string to this:

MT-1,MT-2,MT-3,MT-4

Essentially I want to remove ([]"") characters. Is there a way to do this using Regex commands?
I looked at this link but couldn't find anything about removing special characters.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Cheatsheet

My code so far:

var x = "[("MT-1","MT-2","MT-3","MT-4")]"; 
x = xxx.replace(/[^0-9]+/g, "");
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-special-characters-from-string
Remove special Characters from a String in JavaScript | bobbyhadz
Note that the caret ^ symbol has to be the first character in the square brackets for it to mean "not the following characters". If you use the caret ^ symbol later on in the regex, it will get interpreted as a literal caret ^ symbol.
🌐
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 - const str = 'milk and @#$%&!bread'; // 👇 No 'g' flag in regex const noSpecialChars = str.replace(/[^a-zA-Z0-9 ]/, ''); // 👇 Only first special character removed console.log(noSpecialChars); // milk and #$%&!bread · We can shorten this regular expression a bit with the \w character.
🌐
Codegive
codegive.com › blog › js_regex_remove_special_characters.php
js regex remove special characters: Unlock Flawless Data Cleaning & Input Security
April 6, 2026 - A: To remove all symbols from a string using JavaScript regex, you can use the replace() method with a regular expression like /[^\w\s]/g if you want to keep alphanumeric characters and underscores (\w) and whitespace (\s). If you want strictly alphanumeric and spaces, use /[^a-zA-Z0-9 ]/g. ...
Find elsewhere
🌐
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.
🌐
Medium
nick3499.medium.com › javascript-using-regex-pattern-match-to-remove-repeated-character-from-end-of-string-replace-47870bd095de
JavaScript: Using Regex Pattern Match to Remove Repeated Character from End of String: replace(), regex | by nick3499 | Medium
June 14, 2018 - The simple regular expression /!+$/ is enclosed within forward slashes, / /. The textual character ! will be matched. The special character + matches its preceding textual character multiple times.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-a-character-from-string-in-javascript
Remove a Character From String in JavaScript - GeeksforGeeks
February 25, 2026 - It uses a regular expression with the global flag to target and remove every instance. ... /pattern/g: The regular expression with the g flag (global) to match all occurrences of the specified character or string.
🌐
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. Use /[^a-zA-Z0-9]/g to remove all special characters or create custom patterns for specific requirements.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-characters-not-match-regex
Replace/Remove characters that Don't match Regex in JS | bobbyhadz
If you need to replace each occurrence of a character that doesn't match the regex with a specific character, remove the plus + symbol from the regex.