How do I match negative numbers as well by this regular expression? This regex works fine with positive values, but I want it to also allow negative values ​​eg -10, -125.5 etc.

^[0-9]d*(.d+)?$

Thanks

3

You should add an optional hyphen at the beginning by adding -? (? is a quantifier meaning one or zero occurrences):

^-?[0-9]d*(.d+)?$

I verified it in Rubular with these values:

10.00
-10.00

and both matched as expected.

let r = new RegExp(/^-?[0-9]d*(.d+)?$/);

//true
console.log(r.test('10'));
console.log(r.test('10.0'));
console.log(r.test('-10'));
console.log(r.test('-10.0'));
//false
console.log(r.test('--10'));
console.log(r.test('10-'));
console.log(r.test('1-0'));
console.log(r.test('10.-'));
console.log(r.test('10..0'));
console.log(r.test('10.0.1'));

3

Some regular expression examples:

Positive Integers:

^d+$

Negative Integers:

^-d+$

Integer:

^-?d+$

Positive Number:

^d*.?d+$

Negative Number:

^-d*.?d+$

Positive Number or Negative Number:

^-?d*.{0,1}d+$

Phone number:

^+?[ds]{3,}$

Phone with code:

^+?[ds]+(?[ds]{10,}$

Year 1900-2099:

^(19|20)[d]{2,2}$

Date (dd mm yyyy, d/m/yyyy, etc.):

^([1-9]|0[1-9]|[12][0-9]|3[01])D([1-9]|0[1-9]|1[012])D(19[0-9][0-9]|20[0-9][0-9])$

IP v4:

^(d|[1-9]d|1dd|2[0-4]d|25[0-5]).(d|[1-9]d|1dd|2[0-4]d|25[0-5]){3}$

0

I don’t know why you need that first [0-9].

Try:

^-?d*(.d+)?$

Update

If you want to be sure that you’ll have a digit on the ones place, then use

^-?d+(.d+)?$

1

UPDATED(13/08/2014): This is the best code for positive and negative numbers =)

(^-?0.[0-9]*[1-9]+[0-9]*$)|(^-?[1-9]+[0-9]*((.[0-9]*[1-9]+[0-9]*$)|(.[0-9]+)))|(^-?[1-9]+[0-9]*$)|(^0$){1}

I tried with these numbers and works fine:

-1234454.3435
-98.99
-12.9
-12.34
-10.001
-3
-0.001
-000
-0.00
0
0.00
00000001.1
0.01
1201.0000001
1234454.3435
7638.98701

0

This will allow a - or + character only when followed by a number:

 ^([+-](?=.?d))?(d+)?(.d+)?$

I have some experiments about regex in django url, which required from negative to positive numbers

^(?P<pid>(-d+|d+))$

Let’s we focused on this (-d+|d+) part and ignoring others, this semicolon | means OR in regex, then the negative value will match with this -d+ part, and positive value into this d+

Adding "-" minus followed by ? quantifier in front of [0-9] expression should do the work.

-?[0-9]

For reference

  • ^b^ quantifier matches for any string begin with “b”
  • c?? quantifier matches for 0 or 1 occurrence of “c”
  • [0-9] – find any character between the [] brackets
  • d – find a digit from 0-9
  • d** quantifier matches for zero or more occurrence of “d”
  • . – matches a "." character
  • z++ quantifier matches for one or more occurrence of “z”
  • e$$ quantifier matches for any string end with “e”

Hope, it’ll help to understand posted regex in the question.

This will allow both positive and negative integers

ValidationExpression=”^-?[0-9]d*(d+)?”$”

^[+-]?d{1,18}(.d{1,2})?$

Accepts positive or negative decimal values.

If you have this val="-12XXX.0abc23" and you want to extract only the decimal number, in this case this regex (^-?[0-9]d*(.d+)?$) will not help you to achieve it.
This is the proper code with the correct detection regex:

var val="-12XXX.0abc23";
val = val.replace(/^.|[^-?d.]|.(?=.*.)|^0+(?=d)/g, '');
console.log(val);

Just add a 0 or 1 token:

^-?[0-9]d*(.d+)?$

1

For negative number only, this is perfect.

^-d*.?d+$

Regular expression for number, optional decimal point, optional negative:

^-?(d*.)?d+$;

works for negative integer, decimal, negative with decimal

^(-?d+.)?-?d+$

allow:

23425.23425
10.10
100
0
0.00
-100
-10.10
10.-10
-10.-10
-23425.23425
-23425.-23425
0.234

Not the answer you’re looking for? Browse other questions tagged regex numbers or ask your own question.