i have the following string
foo: 21, bar: 11
where foo and bar are not constants, so I'm trying to match all the digits after the last ":"(colon) character.
const myString = 'foo: 21, bar: 11'
myString.match(/\d+/g).shift().join() -> 11
can i do the same just with pure regex?
thanks!
Using negative regex you can use this regex:
RegEx Demo
(?![^:]*:)
is negative lookahead to assert that there is no:
ahead of digits we are matching.Code Demo:
You may use either of the two solutions:
This one will match up to the last
:
and grab the digits after 0+ whitespaces:And this one will find a
:
followed with 0+ whitespaces, then will capture 1+ digits into Group 1 and then will match 0+ chars other than:
up to the end of string.