khanh
khanh

Reputation: 4606

Regex with last match only

I am trying write a regex to get last digit.

My string: name[0][0].

My regex: str.match(/d+/g)

It return all match. Can you help me make regex return only last match?

Upvotes: 0

Views: 1260

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

To get the last digit,

\d(?=\D*$)

To get the last number.

\d+(?=\D*$)

DEMO

\d+ matches one or more digits. + repeats the previous token or more times. (?=\D*$) called positive lookahead assertion which asserts that the match would be followed by any number of non-digit characters further followed by end of the line.

Upvotes: 2

Related Questions