1

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!

Nico
  • 1,071
  • 1
  • 23
  • 39
  • `"http://myserver.com/_images/1-102332-2234.jpg?w=800&h=400&c=1".match(/\/([^?\/]+)(?=\?|$)/)[1]` – Wiktor Stribiżew Jun 14 '16 at 09:11
  • @WiktorStribiżew why not answer? – gurvinder372 Jun 14 '16 at 09: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](http://stackoverflow.com/questions/511761/js-function-to-get-filename-from-url) here, and perhaps, [this one](http://stackoverflow.com/questions/6543242/how-to-extract-the-filename-of-url-in-javascript) is a good source candidate. – Wiktor Stribiżew Jun 14 '16 at 09:16

2 Answers2

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);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
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.

rplantiko
  • 2,698
  • 1
  • 22
  • 21
Ayyoub
  • 1,315
  • 1
  • 16
  • 37
  • '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. – rplantiko Jun 14 '16 at 10:09
  • Can you update my answer ? with the right explanations – Ayyoub Jun 14 '16 at 12:12
  • Done, I have updated the text (it's marked as "in peer review" right now, so you probably can't see it yet?). – rplantiko Jun 14 '16 at 13:34
  • 1
    Saw it, it take time apparently, I've approved your edit :) Thanks – Ayyoub Jun 14 '16 at 13:41