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.
How to Remove Special Characters in this Javascript Code - Stack Overflow
How do I remove special characters in the specific code below? I've been trying to use replace with var = t, but it has not worked properly. When a person enters a comma in the number they want More on stackoverflow.com
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
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
Stack Overflow
stackoverflow.com βΊ questions βΊ 33236868 βΊ how-to-remove-special-characters-in-this-javascript-code
How to Remove Special Characters in this Javascript Code - Stack Overflow
Removing the comma won't be an issue in the BIRT report that I'm using 2015-10-20T13:45:29.553Z+00:00 ... Well glad to hear that, yeah @Paul-Jan you are right. 2015-10-20T14:02:01.333Z+00:00 ... ParseFloat doesn't support thousand separators. If you want to support user input containing such separators, you'll have to manually strip them off. function toWords(n) { var n=parseFloat(n.replace(',','')).toFixed(2); ...
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 ...
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, '');
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.
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.
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.