-1

Looking for a regex for the following

  1. Should match value before decimal place.(should return 5 if value is 5.4)
  2. Whole number should be matched as it it (2 in case of 2 and 4 in case of 4).

The numbers could be 1.2, 2.4, 2.5566, 3.555, 1, 2, 8

Regex should match 1, 2, 2, 3, 1, 2, 8

Jongware
  • 22,200
  • 8
  • 54
  • 100
Dineshh Bhardwaj
  • 75
  • 1
  • 3
  • 13

1 Answers1

2

You can use this regex:

(?:^|[^.0-9])([0-9]+)

This regex will not match digits that happen after a decimal point, or other numbers that happen after a decimal point.

In other words, it's the same number you would get if you extracted a float and then used Math.floor() (or equivalent).

You can find the result in the first capture group.

Laurel
  • 5,965
  • 14
  • 31
  • 57
  • 1
    @DineshhBhardwaj Please don't add solved to your question title. You need to [accept my answer](http://stackoverflow.com/help/someone-answers) if it helped you. – Laurel Aug 27 '16 at 17:11