Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to match all special characters except a comma

What's the regex values for to match a string against all special characters and letters except a comma.

value = "23,$%aA";

I want to do a match if the value has any pf the special characters and letters like the above string then it will return true but if it just has a value like

value = "23,3456.00"

then it will return false. As all the special characters and letters are no longer part of the string.

Can I do this using match and regex.

like image 788
Chapsterj Avatar asked Nov 15 '25 10:11

Chapsterj


2 Answers

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

var result = str.replace(/[^0-9\.,]/g, "");
like image 176
Ding Avatar answered Nov 17 '25 23:11

Ding


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') };

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!