4

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.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Chapsterj
  • 6,483
  • 20
  • 70
  • 124
  • 1
    For your example of returning false, you have three "types" of characters: digits, a comma, and a period. Are those how you define "letters"? – nickb May 01 '13 at 21:21
  • Have seen?: [What regex will match every character except comma or semi-colon](http://stackoverflow.com/questions/1409162/what-regex-will-match-every-character-except-comma-or-semi-colon?rq=1) – Luigi Siri May 01 '13 at 21:23
  • He wants to keep numbers too though – Ding May 01 '13 at 21:24

2 Answers2

8

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

var result = str.replace(/[^0-9\.,]/g, "");
Ding
  • 3,065
  • 1
  • 16
  • 27
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') };