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
🌐
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 - So the regex matches any character ... and the replace() method returns a new string with all of these characters removed from the original string. The g (global) flag specifies that every occurrence of the pattern should be matched. If we don’t pass a global flag, only the first special character ...
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 ...
🌐
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 - Use replace() with regular expressions to efficiently remove special characters from strings in JavaScript.
🌐
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
🌐
Code Beautify
codebeautify.org › blog › remove-special-sharacters-from-string-javascript
Remove Special Characters From String Javascript
February 22, 2024 - The gi flags in the regular expression ensure a global and case-insensitive match. The matched special characters are then replaced with an empty string, effectively removing them from the original string.
Find elsewhere
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-special-characters-from-string
Remove special Characters from a String in JavaScript | bobbyhadz
We used the plus + symbol to match one or more occurrences of a space and replaced multiple spaces with a single space. An alternative approach to using the caret ^ symbol to specify the characters we want to keep is to specify the special characters we want to remove from the string.
🌐
sebhastian
sebhastian.com › javascript-remove-special-characters-from-string
JavaScript Remove Special Characters From a String | sebhastian
October 25, 2023 - Back with me Nathan in a new article. Today I’m going to show you how to remove special characters from a string in JavaScript. A special character is a character that’s not a letter or a number. 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 ... 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 it) and the safelist in this example is digits, word characters, underscores (\w) and whitespace (\s). ... A simple example code uses a regular expression to replace them with the empty ...
🌐
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 ...
🌐
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 - Let's dive into the article for getting better understanding on how to replace special characters. 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 - I now realise my answer may have ... of the characters specified, to remove multiple instances of ][ from a string I believe this global replace may be more appropriate: ... Base-64 Decoding Error When Calling External API via Flow Designer REST Step in Developer forum Wednesday · (Article3_ TM) Few specific special character ...
🌐
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 - Learn how to remove spaces and special characters from strings in JavaScript using regex and string methods. Clean input easily with simple code.
🌐
Metring
ricardometring.com › javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
Learn a cleaner and more effective way to replace accents, punctuation and other special characters using JavaScript.
🌐
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 - Regular Expressions(Regex) in JavaScript are used to search, find, and replace strings based on specific patterns. 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 - 1. Simple String Replacement Replace the first occurrence of a substring. let str =...
🌐
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 - Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.