Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

regex

I'm trying to find all letters and dashes and dollar signs and remove them from a text box.

function numbersOnly() {     if ($('.sumit').val().indexOf([A-Za-z-$])) {         $('.sumit').val().replace([A-Za-z-$], "");     } } 

That's what I've got and I'm pretty sure it's wrong. I'm not too great with regular expressions, but I'm trying to learn them. Does anyone want to help me out and get me started with completing this function?

So.. You've got the inputs.

<div class="numInputRight"><input type="text" class="sumit" name="sumAmount1"></div> <div class="numInputRight"><input type="text" class="sumit" name="sumAmount2"></div> <div class="numInputRight"><input type="text" class="sumit" name="sumAmount3"></div> 

Then you've got the function:

numbersOnly = function() {   $('.sumit').val().replace(/[A-Za-z$-]/g, "");   alert($('.sumit').val());   return false; }    

I'm alerting to determine if the replace is working. It's not.

like image 266
Michael Stone Avatar asked Jul 23 '09 18:07

Michael Stone


People also ask

How do I use the dollar sign in regex?

If a dollar sign ( $ ) is at the end of the entire regular expression, it matches the end of a line. If an entire regular expression is enclosed by a caret and dollar sign ( ^like this$ ), it matches an entire line. So, to match all strings containing just one characters, use " ^. $ ".

What is the syntax for regular expression?

(dot) Matches any single character, except a new line. Note: Regular expressions in Content Compliance policies are case sensitive. Note: Regular expressions in Content Compliance policies are case sensitive.

Which of the following regular expression matches any single character in brackets?

Use square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.


2 Answers

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.

like image 67
Mark Biek Avatar answered Sep 22 '22 08:09

Mark Biek


Mark's does it for all non-digits. If you want to only take out letters, dashes and $, (but leaving decimals, for example), this modification to your original should do it:

$('.sumit').val().replace(/[A-Za-z$-]/g, ""); 

(And I personally prefer Mark's updated answer for the same reason he does; you catch everything you can't predict that way.)

For your updated question, the reason the values aren't changing is because val() returns a new string. It will not change the actual value. To do that, try:

$('.sumit').each(function() {      $(this).val($(this).val().replace(/[A-Za-z$-]/g, ""));  }); alert($('.sumit').val()); 

I also made it into an each() call so that every element would be done individually.

like image 30
Sean Avatar answered Sep 19 '22 08:09

Sean