Find the last number in the string. A number from 1. A string can end in a number or slash. The response should be only 1 item.

Source line can be:

  1. " http: // domen: 8000 / one / two / tree / 123456789 / "
  2. " http: // domen: 8000 / one / two / tree / 123456789 "
  3. " http: // domen: 8000 / one / two / tree / 1 "
  4. " http: // domen: 8000 / one / 123 / two / 1234567890123456789 / "

Result

  1. 123456789
  2. 123456789
  3. one
  4. 1234567890123456789

My regular expression options

  1. / (?: /) (\ d +) (?: /)? $ /
  2. (?: /) \ d + (?: /)? $

Result

  1. / 123456789 /, 123456789 - 2 elements, only one is needed: 123456789
  2. / 123456789 / - 1 item, but slashes come in response, you need without them

    1 answer 1

    Use

    /\d+(?=\/?$)/ 

    See the regular expression demo .

    Details

    • \d+ - 1 or more digits
    • (?=\/?$) - right after the digits there should be an optional symbol / and the end of the line.