RegEx needed to match number to exactly two decimal places

var regexp = /^[0-9]*(\.[0-9]{0,2})?$/;

//returns true
regexp.test('10.50')

//returns false
regexp.test('-120')

//returns true
regexp.test('120.35')

//returns true
regexp.test('120')

If you're looking for an entire line match I'd go with Paul's answer.

If you're looking to match a number witihn a line try: \d+\.\d\d(?!\d)

  • \d+ One of more digits (same as [0-9])
  • \. Matches to period character
  • \d\d Matches the two decimal places
  • (?!\d) Is a negative lookahead that ensure the next character is not a digit.

^[0-9]*\.[0-9]{2}$ or ^[0-9]*\.[0-9][0-9]$

Tags:

Regex