Remove special characters (like !, >, ?, ., # etc.,) from a string using JavaScript:

var temp = new String('This is a te!!!!st st>ring... So??? What...');
document.write(temp + '<br>');
temp =  temp.replace(/[^a-zA-Z 0-9]+/g,'');
document.write(temp + '<br>');

jsFiddle

Edit:

If you don't want to remove dot(.) from string:

temp =  temp.replace(/[^a-zA-Z 0-9.]+/g,'');
Answer from Naveed on Stack Overflow
🌐
thisPointer
thispointer.com › home › javascript › javascript: replace special characters in a string
Javascript: Replace special characters in a string - thisPointer
June 22, 2021 - In this section, we will be creating a function to replace the special characters in a javascript string. We can use this function to replace all special characters of our choice while retaining the ones we need in the string. ... Replace all occurrences of all special characters with “” (empty) but retain the ‘,’ (comma) and ‘.’ (dots) from the string “Javascript is @ a # language, This is : the most %popular _ language.”
Discussions

javascript replacing special characters - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... is it possible to translate special characters like ®, ü etc with javascript String replace function? More on stackoverflow.com
🌐 stackoverflow.com
javascript - Replace string with special characters for empty string - Stack Overflow
I want to replace all instances of that string in my url to ''. ... I assume it's because of the special characters within the find string itself. More on stackoverflow.com
🌐 stackoverflow.com
Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
Suppose I have a string like this: This is a fridge with 10 chocolates and ~5 icecream samples description to check if backward slash \ breaks anything and checking caret ^ symbol as well I want to replace those special characters with the following: \ to \1F ~ to \7E ^ to \5E I may find more ... More on sitepoint.com
🌐 sitepoint.com
0
April 9, 2024
Remove all special characters except space from a string using JavaScript - Stack Overflow
I want to remove all special characters except space from a string using JavaScript. For example, abc's test#s should output as abcs tests. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to remove specific characters from a string in JavaScript?
To remove specific characters from a string, you can use the .replace() method with a regular expression. Code: let str = "Hello, World!"; let result = str.replace(/[,!]/g, ''); console.log(result); Output: Hello World
🌐
intellipaat.com
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 ...
How to remove space from a string in JavaScript?
To remove space from a string in JavaScript, you can use the .replace() method with a regular expression: Code: let str = "Hello World"; let result = str.replace(/s+/g, ''); console.log(result); Output: HelloWorld
🌐
intellipaat.com
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 ...
How to give space in a string in JavaScript?
We can add space in a string using concatenation or template literals. Code: let str1 = "Hello"; let str2 = "World"; let result = str1 + " " + str2; console.log(result); let result2 = `${str1} ${str2}`; console.log(result2); Output: Hello World Hello World
🌐
intellipaat.com
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 ...
🌐
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, ''). The replace() method will return a new string that ...
🌐
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 replace() method with the regular expression /[^a-zA-Z0-9\s]/g removes all characters that are not letters, numbers, or spaces. The ^ inside the brackets creates a negated character class, meaning it matches anything NOT in the specified range.
🌐
Linux Hint
linuxhint.com › replace-all-special-characters-in-string-in-javascript
How to Replace All Special Characters in a String in ...
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
Find elsewhere
🌐
Code Beautify
codebeautify.org › blog › remove-special-sharacters-from-string-javascript
Remove Special Characters From String Javascript
February 22, 2024 - When working with strings in JavaScript, there might be scenarios where you need to remove special characters to sanitize or process the data. Let’s explore a simple solution to achieve this. 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!
🌐
sebhastian
sebhastian.com › javascript-remove-special-characters-from-string
JavaScript Remove Special Characters From a String | sebhastian
October 25, 2023 - To remove them from a string, you need to use the replace() method that’s available for string values. Add a regular expression as the part of string you want to replace, then pass an empty ...
🌐
EyeHunts
tutorial.eyehunts.com › home › replace all special characters in javascript | example code
Replace all special characters in JavaScript | Example code
June 27, 2022 - Use replace method with a regular expression to replace all special characters in JavaScript. ... 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 ...
🌐
SitePoint
sitepoint.com › javascript
Efficient way of replacing special characters - JavaScript - SitePoint Forums | Web Development & Design Community
April 9, 2024 - Suppose I have a string like this: This is a fridge with 10 chocolates and ~5 icecream samples description to check if backward slash \ breaks anything and checking caret ^ symbol as well I want to replace those special characters with the following: \ to \1F ~ to \7E ^ to \5E I may find more to replace in future but at this point I just want to handle above such that after replacing it, the string looks like the following: This is a fridge with 10 horse and \7E5 goat samples descriptio...
🌐
TutorialsPoint
tutorialspoint.com › how-to-replace-all-the-special-characters-following-another-character-ndash-javascript
JavaScript regex - How to replace special characters?
April 21, 2023 - For this we use replace() method. 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.
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-can-i-replace-special-character-with-empty-string › m-p › 2446274
Solved: Re: How can I replace special character with empty... - ServiceNow Community
January 18, 2023 - Hi, For me, validated in script background: sys_id = str.replace(/[\[\]]/, ''); Validation was var str1 = '22122b37c611228400f9ff91c857581d]'; var str2 = '[aaccc971c0a8001500fe1ff4302de101'; var newStr; newStr = str1.replace(/[\[\]]/, ''); gs.info(newStr); newStr = str2.replace(/[\[\]]/, '');...
🌐
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. For simple tasks like removing spaces, string methods like trim() or split()/join() can be sufficient and more readable. Cleaning strings in JavaScript is straightforward with regex or string methods.
🌐
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. ... Another quite recurrent use case is the need to clear the accents and then replace special characters with some other one, e.g.
🌐
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 - We can also use regex to remove special characters from a string while keeping space. We have to use the replace() function to replace all the special characters with empty strings.
🌐
DEV Community
dev.to › maafaishal › javascript-stringreplace-useful-cases-3963
JavaScript `string.replace()` useful cases - DEV Community
September 24, 2024 - To replace characters that aren't ... = str.replace(/\d{3}/g, "***"); // Output: "Contact: ***-***-***0" You can also use Unicode escape sequences to replace special characters....