2

I heard that there are two persons in the world: Those who know how to regex and those who will never know. I think I'm the type 2 :-/

I have this url:

http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1 

And I need an regex which extracts only the filename 1-102332-2234.jpg

The file ending ca be everything with a . and the parameter "?w=..." is not necessary.

We are trying to get it to work for 2 days now... any help would be appreciated!

3
  • "http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/\/([^?\/]+)(?=\?|$)/)[1] Commented Jun 14, 2016 at 9:11
  • @WiktorStribiżew why not answer? Commented Jun 14, 2016 at 9:13
  • 1
    @gurvinder372: I believe this question is a dupe of many others. If I find a good source, I will flag this one. I do not like answering evident dupes.One here, and perhaps, this one is a good source candidate. Commented Jun 14, 2016 at 9:16

2 Answers 2

8

try this regex

/[\w-]+\.jpg/g

and to support more file extensions

/[\w-]+\.(jpg|png|txt)/g

var output = "http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/[\w-]+\.jpg/g);

console.log(output);

//covering other file extensions
var output = "http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/[\w-]+\.(jpg|png|txt)/g);

console.log(output);

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

2 Comments

. inside the regex matches any char but a newline, you meant to escape it. g+ matches 1 or more g letters, why?
@WiktorStribiżew Thanks, you are right as usual :). I have incorporated your suggestions.
7

I take the answer of @gurvinder372 and try to explain it for better understanding

/[\w-]+.(jpg|png|txt)/g

To explain what does the regex do :

/ : Delimiter, denoting the beginning and the end of the regex

[\w-] : the brackets [ and ] enclose the ad hoc definition of a set of characters. In this case, the set firstly should include all the characters of the predefined character set \w, defined to be all alphanumeric characters and the underscore; additionally, the character set should contain the hyphen - which does not belong to the predefined character set \w

+ : is one of several possible quantifiers following the definition of a character set, it says: "at least one occurrence of elements of the specified character set" (other quantifiers are * = 0, 1, or any more occurrences, ? = 0 or 1 occurrences, and {n,m}= at least n, at most m occurrences.

.: the dot means an arbitrary ("any") character. What was really meant here was \., the character .. The "any" specifier works by chance here, since the dot is a character!

(jpg|png|txt): alternation: following the . arbitrary character, there should occur either one of the character sequences separated by the vertical bar.

/g : after the delimiter / finishing the regular expression, several modifiers are possible. The modifier g here instructs functions like match() to look for as many subsequences of the string as possible that match the regex. If it would be ommitted, the regex engine would stop searching after having found the first match of the pattern. Actually, this would be completely sufficient in this case, as the URL is supposed to contain only one filename, so it can stop after having found the match.

4 Comments

'Find the character "/" ist' wrong. The leading / is only the delimiter of the regex. The word "concat" is misleading, the bar inside of parentheses denotes alternatives. Also, the g modifier is not necessary, since the URL probably contains only one image filename.
Can you update my answer ? with the right explanations
Done, I have updated the text (it's marked as "in peer review" right now, so you probably can't see it yet?).
Saw it, it take time apparently, I've approved your edit :) Thanks

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.