calebo calebo - 25 days ago 11
Javascript Question

Get end of string that matches pattern with lodash

How can I get the last integer(s) at the end of the string including the dash before it using lodash?

'hello-world-bye-945'


So the end result is just
-945
.

Answer

Use JavaScript String#match method

console.log(
  'hello-world-bye-945'.match(/-\d+$/)[0]
)

Comments