Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript match anything until pattern

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading