1

Say I've got an URL:

http://www.example.com/foo/bar/yes_no_no.html

I'm looking for a regex that can extract all the characters of the filename up to the first underscore. So in the above URL I want to extract "yes".

So far I managed to get the whole "yes_no_no" bit using:

/([^\/]+)(?=\.\w+$)/, but I can't seem to get it to only match "yes".

1 Answer 1

3

Try this:

/\/([^_\/]+)_?[^\/]*\.[a-z]+$/

Example:

console.log('http://www.example.com/foo/bar/yes_no_no.html'.match(/\/([^_\/]+)_?[^\/]*\.[a-z]+$/)[1])
console.log('http://www.example.com/foo/bar/yes.html'.match(/\/([^_\/]+)_?[^\/]*\.[a-z]+$/)[1])

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.