Chapsterj
Chapsterj

Reputation: 6625

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.

Upvotes: 4

Views: 15203

Answers (2)

user2279235
user2279235

Reputation:

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

Upvotes: 1

Ding
Ding

Reputation: 3085

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

var result = str.replace(/[^0-9\.,]/g, "");

Upvotes: 8

Related Questions