1

Hi I have a varying URL similar to: http://farm4.staticflickr.com/3877/[image_id]_[secret].jpg

e.g. http://farm4.staticflickr.com/3877/14628998490_233a15c423_q.jpg

I need to extract image_id that's first set of numbers (i.e. 14628998490) before an underscore from 14628998490_233a15c423_q.jpg between the whole URL

Is there a good way to extract image_id?

Right now I am going to use:

var image_id = image_url.match(/[\/]([0-9]+)_/)[1]
9
  • 6
    You're answering your own question. That regex works, doesn't it? Commented Aug 3, 2014 at 12:35
  • 1
    you don't need to escape / in the character class. you don't need even a character class also. Just \/ would be enough. Commented Aug 3, 2014 at 12:44
  • @PetervanderWal OP's regex would works only if the part represented by image_id contains numbers. It won't works if it contains alphanumeric charcters or anything else. Commented Aug 3, 2014 at 13:06
  • @AvinashRaj As stated by OP, it is "that's first set of numbers". Commented Aug 3, 2014 at 13:21
  • Ya, he means only in this example. Commented Aug 3, 2014 at 13:24

4 Answers 4

3

Like i said in the comment, you don't need to escape / symbol in the character class. And also you don't need even a character class also. Just \/ would be enough. The below regex would capture one or more numbers which are preceded by / symbol and followed by _ symbol.

\/(\d+)_

DEMO

> var image_id = image_url.match(/\/(\d+)_/)[1]
undefined
> image_id
'14628998490'

OR

You could try this also, if you don't want to give \d+ in your pattern.

\/([^/]*?)_

DEMO

> var image_id = image_url.match(/\/([^/]*?)_/)[1]
undefined
> image_id
'14628998490'
Sign up to request clarification or add additional context in comments.

2 Comments

How is this any different from the OP's original regex?
He wants to shortdown his regex. Don't need to escape / in the char class.
1

Not shure that it's is better way, but you can do like this:

var str = 'http://farm4.staticflickr.com/3877/[image_id]_[secret].jpg';
var image_id = str.split('/').pop().split('.')[0].split('_');

Comments

1

If the special character is always the same (_), you could first obtain the last part (width substring+lastIndexOf) and then use split() :

var url = "http://farm4.staticflickr.com/3877/14628998490_233a15c423_q.jpg";
var splittedUrl = url.substr(url.lastIndexOf('/')+1).split("_");
var image_id = splittedUrl[0];
console.log(image_id);

I've read somewhere that string functions are faster than regexp, so it's an option you might consider.

Comments

0

String splitting is faster tha regex.You can just get the last index of / and string between first occurence of _ after last occurence of /. I think that will be better idea.

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.