Eric David Eric David - 1 month ago 10
Javascript Question

Regular expression to read only digits after last slash

I'm trying to read the numbers after the last \ using regular expressions but unable to do it.

I've tried using

[^\\]*$
,
([^/]+$)
but both don't work. The first one removes the numbers.

Please can you advice?

My sample data is:

C:\Users\Documents\Projects\Austraila\Customer\Organisation\176276

Answer

In JS, you may use

/\\(\d+)$/

See the regex demo

The \\(\d+)$ regex matches:

  • \\ - a literal \ symbol
  • (\d+) - Group 1: one or more digits
  • $ - end of string.

var path = "C:\\Users\\Documents\\Projects\\Austraila\\Customer\\Organisation\\176276";
var m = path.match(/\\(\d+)$/);
if (m) {
  console.log(m[1]);
}

Lua solution:

print(
    string.match(
        [[C:\Users\Documents\Projects\Austraila\Customer\Organisation\176276]], 
        [[\(%d+)$]]
    )
)