This will match everything that is not numeric or not a comma or period (decimal point)

var result = str.replace(/[^0-9\.,]/g, "");
Answer from Ding on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ javascript-regex-how-to-replace-special-characters
JavaScript regex - 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.
Discussions

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
Replacing space and comma from a string using regex
I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: โ€œ23,45,567โ€.replace(โ€˜/[1]$/gโ€™, โ€˜โ€™) โ€œ23,45,567โ€.replace(โ€˜/[2]+$/gโ€™, โ€˜โ€™) โ€œ23,45,567โ€.replace(โ€˜/^\s,$/gโ€™, โ€˜โ€™) And many ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
3
0
October 26, 2020
Javascript replace special characters - Stack Overflow
Let's say I want to replace this: Test Test into this: Test\X20\Test I know this sounds weird but I need for the string to look like that. And I can't get my head around this because of the l... 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
๐ŸŒ
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.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ javascript replace all word | space, comma | special characters
How to JavaScript replace all Word | Space, Comma | Special Character
May 15, 2021 - <!DOCTYPE html> <html> <script> var string = 'EyeHunts Tutorial JS'; var newstring = string.replace(/JS/, 'Javascript'); document.write(newstring); </script> <body> </body> </html> ... The best way is to use regular expression with g (global) flag. ...
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ replace
String.prototype.replace() - JavaScript - MDN Web Docs
The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ programming โ€บ javascript
Replacing space and comma from a string using regex - JavaScript - The freeCodeCamp Forum
October 26, 2020 - I may get a string like this: 23,45,567 23,45 56 7 I want it to free of all the commas and spaces. So, I tried to use the following codes: โ€œ23,45,567โ€.replace(โ€˜/[1]$/gโ€™, โ€˜โ€™) โ€œ23,45,567โ€.replace(โ€˜/[2]+$/gโ€™, โ€˜โ€™) โ€œ23,45,567โ€.replace(โ€˜/^\s,$/gโ€™, โ€˜โ€™) And many more like this.
Find elsewhere
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ javascript-remove-special-characters-from-string
Remove special Characters from a String in JavaScript | bobbyhadz
Use the `replace()` method to remove all special characters from a string, e.g. `str.replace(/[^a-zA-Z0-9 ]/g, '');`.
๐ŸŒ
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...
๐ŸŒ
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 in the ASCII range, you can use Unicode properties. let str = "Hรฉllo Wรถrld"; let result = str.replace(/[^\x00-\x7F]/g, ""); // Output: "Hllo Wrld" You can replace digits (or groups of digits) using regular ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ programming
Removing special characters from a string - Programming - The freeCodeCamp Forum
July 30, 2017 - Currently working on making a palindrome function. I have the gist of it. Just canโ€™t get this last part. Iโ€™ve tried so many examples from so many stockoverflow posts and this still doesnโ€™t work for me. Can someone help me figure out whatโ€™s wrong? var str = "abc's test#s"; str.replace(/[&\/\\#,+()$~%.'":*? {}]/g,'_'); console.log(str);
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 67499160 โ€บ replace-comma-with-special-characters-in-javascript
node.js - Replace comma with special characters in Javascript - Stack Overflow
I don't know your use case, but "\" isn't a valid URL character unless it's properly encoded. See this post. ... @DrewReese Yeah.. I know adding "\" will make it invalid URL, but I want to consider it as string. Actually my intention here is to escape comma, so to do that I wanted to add '\' before comma.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ replace-commas-with-javascript-regex
Replace commas with JavaScript Regex?
We want to replace the last comma in each string with " JavaScript". The regular expression /,(?=[^,]*$)/ matches a comma that is followed by any characters except commas until the end of the string, effectively targeting the last comma.
๐ŸŒ
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 - Use the JavaScript replace() method with RegEx to remove a specific character from the string. The example code snippet helps to remove comma (,) characters from the start and end of the string using JavaScript.
๐ŸŒ
Metring
ricardometring.com โ€บ articles โ€บ javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
April 12, 2019 - Then the method replaces all occurrences of diacritical characters, combining them in the Unicode sequence \u0300 - \u036F, another advantage of ES6 that was added to allow Unicode ranges in RegEx. To remove the accents and other special characters like /?!(), just use the same formula above, only replace everything but letters and numbers.
๐ŸŒ
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.