-1

I have a string with multiple URLs and other content in it.

I have URLs in following format:

http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG

The part till http://www.example.com/bla1/bla2/thumb_ is fixed.

However, I dont know what will be: my-file-name(2). I know it will be ending with .JPG

So far, I'm using the following regex to detect them:

/http:\/\/www.example.com\/bla1\/bla2\/thumb_/g

and I have also used \.JPG to detect the end.

But how do I capture the middle part as well? How do I catch this whole URL?

Dushyant Bangal
  • 6,048
  • 8
  • 48
  • 80
  • please post the code. Is this express server? – smnbbrv Aug 29 '17 at 05:41
  • I'm sending a request from nodejs. Need to find the URLs in the received response. Thats it. – Dushyant Bangal Aug 29 '17 at 05:43
  • I dont understand the downvotes. I've explained everything in the description, posted example and my existing expression. Couldn't find a better title. – Dushyant Bangal Aug 31 '17 at 10:24
  • well you did not post enough code and forced others that want to help you to guess what you mean and what stack you do have behind the question etc. This is annoying when you are on the answerer's side => you get downvotes (I did not make any, just trying to explain why I would do that) – smnbbrv Aug 31 '17 at 10:51
  • What else is there to post? I posted my input, the regex that I'm using on it, and the output I need! Its just `input.exec(regEx)` – Dushyant Bangal Sep 06 '17 at 07:46

1 Answers1

0

Try this regex pattern:

http://www.example.com/bla1/bla2/thumb_(.*?)\.JPG

Code:

var url = "http://www.example.com/bla1/bla2/thumb_my-file-name(2).JPG";
var myRegexp = /http:\/\/www.example.com\/bla1\/bla2\/thumb_(.*?)\.JPG/g;
var match = myRegexp.exec(url);
console.log(match[1]); // my-file-name(2)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Hah! works! I had tried `\[(.*?)\]` :D – Dushyant Bangal Aug 29 '17 at 05:48
  • yes, sorry for the delay in marking it as answer. It didnt let me do immediately, and wasn't online for last few days. I dont understand the downvotes though – Dushyant Bangal Aug 31 '17 at 10:19
  • I also don't understand the downvotes, it seems overly harsh to me. – Tim Biegeleisen Aug 31 '17 at 10:28
  • I posted my input, the regex that I'm using on it, and the output I need! What matters is the expression, and not `input.exec(regEx)`. Wish they made people specify reason for downvote so the OP can atleast know what needs to be refined. :( – Dushyant Bangal Sep 06 '17 at 07:50
  • 1
    @DushyantBangal I upvoted your question and honestly don't think it's an exact duplicate. But I don't have a gold medal in regex so I cannot reopen it. – Tim Biegeleisen Sep 06 '17 at 08:27