Regex for number

14,945

You can use this regex to strip out any non-numeric or . characters: /[^\d\.]/g

So:

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

The way this regex works is as follows:

/  -> start of regex literal
[  -> start of a "character class". Basically you're saying I to match ANY of the
      characters in this class.
^  -> this negates the character class which says I want anything that is NOT in 
      this character class
\d -> this means digits, so basically 0-9.
\. -> Since . is a metacharacter which means "any character", we need to escape 
      it to tell the regex to look for the actual . character
]  -> end of the character class
/  -> end of the regex literal.
g  -> global flag that tells the regex to match every instance of the pattern.

So basically this looks for anything that is NOT a digit or a decimal point.

To see if it is in the right format, you can check to see if the value matches:

/\d*(\.\d{0, 2})?/

You can use it like so:

if(/^\d*(\.\d{0, 2})?$/.test($('#new_price').val()) {
   ...
   ...
}

So the code in the if block will run only if it matches the pattern. Here is an explanation of the regex:

/        -> start of regex literal
^        -> anchor that means "start of string". Yes the ^ is used as an anchor
            and as a negation operator in character classes :) 
\d       -> character class that just matches digits
*        -> this is a regex metacharacter that means "zero or more" of the 
            preceding. So this will match zero or more digits. You can change
            this to a + if you always want a digit. So instead of .99, you want
            0.99.
(        -> the start of a group. You can use this to isolate certain sections or
            to backreference.
\.       -> decimal point
\d{0, 2} -> zero to two numbers.
)        -> end of the group
?        -> regex metacharacter that means "zero or one" of the preceding. So 
            what this means is that this will match cases where you do have 
            numbers after the decimal and cases where you don't.
$        -> anchor that means "end of string"
/        -> end of the regex literal
Share:
14,945

Related videos on Youtube

Richard Newman
Author by

Richard Newman

Updated on September 15, 2022

Comments

  • Richard Newman
    Richard Newman over 1 year

    I'm fairly new to reg expressions and I'm struggling to come up with a regex for a price field.

    I want to use

    var price = $('#new_price').val().replace(/*regex*/, '');
    

    to strip out any unwanted characters.

    The price can be an int ie 10 or a decimal to 2dp ie 9.99

    basically I want anything that doesn't match a standard monetary format to be removed.

    OR

    I'd like a regex to check (using .match(regex)) that the field is in the desired format.

    If someone could quickly write either of those regex's for me and explain it to me so i know for the future I would be very grateful.

  • Sergiu Mindras
    Sergiu Mindras almost 9 years
    {0, 2} replace with {0,2} (no spaces) this messes chrome up apparently