-3

I am trying to write up a regular expression that accepts numbers, decimals as well as an empty string / null value.

My current expression accepts decimal values that are dot separated. The numbers can start with either a - (minus) or + (plus). Additionally, the value can start with a decimal point - for instance: .56

Here is the expression:

/^([-+]?(\d+|\.\d+|\d+\.\d*))?$/

How can I refactor it to accept an empty string/ null value?

kajoe14
  • 23
  • 5

1 Answers1

-1

You can write something like this :

/^(?:[-+]?(\d+|.\d+|\d+.\d*))?$/

or maybe just append null using pipe:

/^(?:[-+]?(\d+|.\d+|\d+.\d*))?$/|null

Velocity
  • 433
  • 5
  • 32