Regular expressions [xkcd] (I feel like him ;)):

s = s.replace(/[\[\]&]+/g, '');

Reference:

  • MDN - string.replace
  • MDN - Regular Expressions
  • http://www.regular-expressions.info/

Side note:

JavaScript's replace function only replaces the first occurrence of a character. So even your code would not have replaced all the characters, only the first one of each. If you want to replace all occurrences, you have to use a regular expression with the global modifier.

Answer from Felix Kling on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ remove-all-occurrences-of-a-character-in-a-string-using-javascript
JavaScript - Remove all Occurrences of a Character in JS String - GeeksforGeeks
July 23, 2025 - In this approach, Using the split() method, we break a string into an array at each occurrence of a specified character. Then, with the join method, we reconstruct the array into a new string, effectively removing that character.
Discussions

javascript - How to remove all characters from a string - Stack Overflow
How can I remove all characters from a string that are not letters using a JavaScript RegEx? More on stackoverflow.com
๐ŸŒ stackoverflow.com
jquery - Remove all characters from string javascript - Stack Overflow
The replace() function doesn't seem to remove the characters if you for example insert "10.00" in the input field. I would like it to remove the dot before reformatting it with the "10:00". ... I've just updated the the code with your suggestion and if you write "10.00" and then click somewhere else you get "10:.0". It should generate "10:00". ... You've specified a string ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 20, 2013
removing all non-letter characters from a string? ((using regex))
This is very simple with regex. You just need to replace non-words (/\W/ig). So: var string = "lakjsdlkasjdlsaj@ยฃ$%^&*klajdlaskjds"; string.replace(/\W/ig, ""); --> "lakjsdlkasjdlsajklajdlaskjds" \w == words, \W == non words. You don't need a loop here as we're using /g which stands for global, so we replace every instance. And just incase I'm using /i as well which ignores the case. As it's regex you can just do /ig and the selectors will stack to ignore case and global. My favorite regex visualiser lives here: https://jex.im/regulex/#!embed=false&flags=ig&re=%5CW More on reddit.com
๐ŸŒ r/learnjavascript
10
4
September 28, 2015
Powershell - Remove all characters except...
You can use regex and a character class to tell -replace what you want to keep or remove. [a-zA-Z0-9-\\] Using a character class you can tell it to match any of the characters within [ ] literally [a-zA-Z] - is the range of characters in lower and upper case [0-9] - is the range of numbers from 0-9 [-] - is the hyphen character literally [\\] - is the backslash character which is a special character so needs to be escaped with another backslash. Putting them all together and using this would be: 'This-is-a\string:with(lo*ts&ofยฃ$stu-=ff' -replace '[a-zA-Z0-9-\\]' To tell it to replace everything NOT in your character class you prefix it with a carat '[^a-zA-Z0-9-\\]' More on reddit.com
๐ŸŒ r/PowerShell
10
4
March 12, 2019
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-remove-characters-from-a-string-in-javascript
How to remove characters from a string in JavaScript
JavaScript provides several built-in methods for removing characters from strings, each with unique functionalities: replace(): Replaces specified characters with a new string or removes them when an empty string is used. To replace all occurrences, combine it with a regular expression and the global flag.
๐ŸŒ
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
๐ŸŒ
Quora
quora.com โ€บ How-do-you-remove-all-occurrences-of-a-character-from-a-string-in-JavaScript
How to remove all occurrences of a character from a string in JavaScript - Quora
Answer (1 of 2): In JavaScript there is no character type, itโ€™s all just strings. So, you would just replace the substring consisting of your one character with an empty string, effectively removing it from the original string. To replace all occurrences of a substring in another string, ...
๐ŸŒ
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 ("") ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to remove a character from string in javascript?
How to Remove a Character from String in JavaScript? - Scaler Topics
June 28, 2022 - The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the ...
Find elsewhere
๐ŸŒ
Stackademic
stackademic.com โ€บ blog โ€บ javascript-how-to-remove-a-character-from-a-string
JavaScript: How to Remove a Character From a String | Stackademic
August 29, 2023 - However, itโ€™s important to note that only the first occurrence of the character is removed. To remove all occurrences of a character, you can use a regular expression with the replace() method.
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-basic-exercise-135.php
JavaScript basic: Remove all characters from a given string that appear more than once - w3resource
July 2, 2025 - /** * Function to remove duplicate characters from a string * @param {string} str - The input string * @returns {string} - The string without duplicate characters */ const remove_duplicate_chars = (str) => { // Split the string into an array of characters const arr_char = str.split(""); const result_arr = []; // Loop through each character of the array for (let i = 0; i < arr_char.length; i++) { // Check if the first and last occurrence of a character in the string are the same if (str.indexOf(arr_char[i]) === str.lastIndexOf(arr_char[i])) result_arr.push(arr_char[i]); } // Join the array of u
๐ŸŒ
Envato Tuts+
code.tutsplus.com โ€บ home โ€บ coding fundamentals
How to Remove a Character From a String in JavaScript | Envato Tuts+
June 26, 2021 - So thatโ€™s how you can use the replace method with a regular expression to remove all occurrences of a specific character from the source string. In this last section, weโ€™ll see how you can use the split method to remove a character from ...
๐ŸŒ
TraceDynamics
tracedynamics.com โ€บ javascript-remove-character-from-string
11 Ways to Remove Character from String in JavaScript
January 9, 2024 - The JavaScript substring() method retrieves characters between two indexes, returning a new substring. By setting startindex and endindex, you can effectively remove characters. For instance, to remove first character from a string, you can ...
๐ŸŒ
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, '').
Top answer
1 of 3
58

You can use the replace method:

'Hey! The #123 sure is fun!'.replace(/[^A-Za-z]+/g, '');
>>> "HeyThesureisfun"

If you wanted to keep spaces:

'Hey! The #123 sure is fun!'.replace(/[^A-Za-z\s]+/g, '');
>>> "Hey The sure is fun"

The regex /[^a-z\s]/gi is basically saying to match anything not the letter a-z or a space (\s), while doing this globally (the g flag) and ignoring the case of the string (the i flag).

2 of 3
12

RegEx instance properties used g , i

global : Whether to test the regular expression against all possible matches in a string, or only against the first.

ignoreCase : Whether to ignore case while attempting a match in a string.

RegEx special characters used [a-z] , +

[^xyz] : A negated or complemented character set. That is, it matches anything that is not enclosed in the brackets. You can specify a range of characters by using a hyphen.

For example, [abcd] is the same as [a-d]. They match the 'b' in "brisket" and the 'c' in "chop".

+ : Matches the preceding item 1 or more times. Equivalent to {1,}.

JavaScript string replace method syntax

str.replace(regexp|substr, newSubStr|function[, Non-standard flags]);

The non-standard flags g & i can be passed in the replace syntax or built into the regex. examples:

var re = /[^a-z]+/gi;   var str = "this is a string";   var newstr = str.replace(re, "");   print(newstr);

var str = "this is a string";   var newstr = str.replace(/[^a-z]+/, "", "gi");   print(newstr);

To match whitespace characters as well \s would be added to the regex [^a-z\s]+.

JavaScript Reference

๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 1279-how-to-remove-a-character-from-a-string-in-javascript
[JavaScript] - How to Remove a Character from a String in | SheCodes
Summer Coding Sale ๐ŸŽ‰ 30% off on all coding workshops ending on June 22nd Ending in 3 days Get Deal Get This Deal NOW ... Learn the easiest way to remove a character from a string in JavaScript using the replace() function.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ remove-characters-strings-js
Linux Hint โ€“ Linux Hint
October 6, 2021 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ remove-all-occurrences-of-a-character-in-a-string-using-javascript
JavaScript โ€“ Remove all Occurrences of a Character in JS String | GeeksforGeeks
November 21, 2024 - Using String slice() MethodThe slice() method returns a part of a string by specifying the start and end indices. To remove the last character, we slice the string from the start (index 0) to the second-to-last charact ...
๐ŸŒ
MSR
rajamsr.com โ€บ home โ€บ javascript remove character from string: 11 easy ways
JavaScript Remove Character from String: 11 Easy Ways | MSR - Web Dev Simplified
February 24, 2024 - Learn how to use JavaScript to remove a character from a string with different methods and examples in this tutorial.