-1

I am trying to create a regex that will extract the bold portion from a Flickr URL.

I want the string between www.flickr.com/photos/ and /.

https://www.flickr.com/photos/annaheimkreiter/14785981533/in/explore-2014-07-28
                              ^^^^^^^^^^^^^^^

I have looked at various answers here and have not been able to adapt them to this aim.

CodeToad
  • 4,656
  • 6
  • 41
  • 53
  • 1
    I changed the formatting to make this easier to read, but I didn't try to fix the inconsistency between the question and the example. What do you want to do about the `photos/` directory? – Alan Moore Jul 29 '14 at 22:47
  • I corrected the mistake. IT should be www.flickr.com/photos/ – CodeToad Jul 30 '14 at 08:18

1 Answers1

2

You can use:

var s = 'https://www.flickr.com/photos/annaheimkreiter/14785981533/in/explore-2014-07-28';
var t;
if (m=s.match(/\/photos\/([^\/]+)/))
   t=m[1];
//=> annaheimkreiter
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    +1, a capture group is the way to go. Maybe the downvote is because it's not 100% clear from the question that `annaheimkreiter` is what CodeToad wants: on the one hand it is in bold; on the other hand he says he wants the string after `.com`, which would be `photos`. (In that case Code Toad it's a tiny tweak to anubhava's regex: `m=s.match(/\/flickr\.com\/([^/]+)/)`) – zx81 Jul 29 '14 at 21:16
  • I agree that there's no call for lookbehind here (emulated or otherwise), but you should have included a comment to that effect in the answer. Also, don't you need to escape the slash in `[^/]`?. It's the regex delimiter, not a regex metacharacter. – Alan Moore Jul 29 '14 at 22:58
  • Actually inside the character class it doesn't need escaping. I tested on Chrome. But may be some older browsers may complain that's why I edited it. – anubhava Jul 30 '14 at 04:22
  • This is close. see here: http://regexr.com/3983a I want the result to be only 'annaheimkreiter' – CodeToad Jul 30 '14 at 08:22
  • Yes result is indeed just `annaheimkreiter` using code I showed above.' – anubhava Jul 30 '14 at 08:41
  • [See this demo](http://regex101.com/r/yT8qY2/2) and your text is in the bottom-right `MATCH INFORMATION` – anubhava Jul 30 '14 at 08:44