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:
/^(?!0d)d+.?d*$/
but it doesn’t work well.
Advertisement
Answer
To modify the regex and accept only a - as well, you could make 2 optional parts, and assert that the string is not empty.
^(?!-?0d|$)-?(?:d+.?d*)?$
^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