I believe the best template would be:
/^[^\
which breaks out like:
^ - start of string [^\
The reason for this is that [^ \?] + Is greedy because it will try to match as long as possible, which covers either a question mark followed by arbitrary characters, or nothing, since all the characters to the end of the line are already written in the group capture without question marks.
So using
var RE=new RegExp(/^[^\#\?]+\/media-group\/([^\?]+).*$/), url="image/media-group/rugby-league-programme-covers-3436?sort=title"; console.log(url.match(RE)[1])
prints: rugby-league-programme-covers-3436 and changing the url to image/media-group/rugby-league-programme-covers-3436 gives the same result.
Update
Changed the description of the David Furster template.
source share