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 Top answer 1 of 2
8
This will match everything that is not numeric or not a comma or period (decimal point)
var result = str.replace(/[^0-9\.,]/g, "");
2 of 2
1
var check = yourString.match(/[^0-9,\.]/);
Here check will be 'null' if the string does not contain a character different to a number, a comma or a point. If the string has any of these characters, check will be an Array. You could test this in this way
if (check === null ) { console.log('No special characters present') };
if (typeof check === 'Array' ) { console.log('Special characters present') };
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.
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
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
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
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
04:48
Remove All Special Characters from String โ JavaScript Regex ...
04:05
How to remove special characters from a given string using JavaScript.
01:45
How do I remove special characters in a string in JavaScript? - ...
03:00
How can I remove a specific character from a string in JavaScript?
16:46
Javascript Tutorial 42: Special Characters In Regular Expressions ...
02:17
Replace All Special Characters in JavaScript - YouTube
Top answer 1 of 13
570
You should use the string replace function, with a single regex. Assuming by special characters, you mean anything that's not letter, here is a solution:
const str = "abc's test#s";
console.log(str.replace(/[^a-zA-Z ]/g, ""));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
2 of 13
211
You can do it specifying the characters you want to remove:
string = string.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
Alternatively, to change all characters except numbers and letters, try:
string = string.replace(/[^a-zA-Z0-9]/g, '');
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.
Metring
ricardometring.com โบ javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
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.
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...
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.