Given example-123, I need to extract just example. Also example could be: example-example-345, in which case I need example-example. This is the pattern I’m looking for:
> str.match('-[0-9]{1,}$')[0]
'-123'
I tried:
str.match(/(.*)-[0-9]{1,}$/)
'example-123'
and
str.match(/(?s).*)-[0-9]{1,}$/)[0]
Uncaught SyntaxError: Invalid regular expression: /(?s).*)-[0-9]{1,}$/: Invalid group
and
str.match('[^-[0-9]{1,}$]')
null
and
str.match('(.*)[^-[0-9]{1,}$]')
null
and
str.match('/.*/[^-[0-9]{1,}$]')
null
and
str.match('.*^(-[0-9]{1,}$)')
null
… the list goes on
>Solution :
Try this:
str.match(/^(.+)-\d+$/)[1]
matching everything till a hyphen followed by numbers!
And with [1] it will get the first part only!