Skip to content

do you know how to modify the regex to allow one ‘-‘ char at the beginning of the string

i have the existing regex pattern: /^-?(?!0d)d+.?d*$/,

Currently the above regex do not allow – char at beginning of the string

Do you know how to modify the regex to also allow one ‘-‘ input besides the above existing validation My attempt:


but it doesn’t work well.


To modify the regex and accept only a - as well, you could make 2 optional parts, and assert that the string is not empty.

  • ^ Start of string
  • (?!-?0d|$) Assert not optional - 0 and digit or end of string
  • -? Match optional -
  • (?:d+.?d*)? Optionally match 1+ digits, optional . and 0+ digits
  • $ End of string

Regex demo