How do I remove letters and dashes and dollar signs in a string using a regular expression?

This should do it

$('.sumit').val().replace(/[^\d.]/g, "");

The [^] is a negated character class so it’s going to match all characters except those listed in the character class.

In this case, our character class is \d (which is the numbers 0-9) and a period (to allow decimal numbers).

I prefer this approach because it will catch anything that’s not numeric rather than having to worry about explicitly listing the non-numeric characters I don’t want.

If you really only want to exclude letters, $, and -, then Sean’s answer is a better way to go.

Leave a Comment