user2093301
user2093301

Reputation: 367

Regex to get src of iframe element

I am trying to retrieve part of the src from different iframes from an HTML input.

So far, I've tried different methods but none of them works for all iframes. What I've tried so far:

<iframe(.*?)><\/iframe>
<iframe src="(.+?)".+</iframe>
<iframe.+?src=[\"'](.+?)[\"'].*?>

And here is a sample of iframe tags that I have:

<iframe src="http://www.youtube.com/embed/NM51qOpwcIM?modestbranding=1;rel=0;showinfo=0;autoplay=0;autohide=1;yt:stretch=16:9;wmode=transparent;?wmode=transparent" allowfullscreen="" style="width: 640px; height: 361.057px;" frameborder="0"></iframe>

<iframe src="https://www.youtube.com/embed/VASywEuqFd8?feature=oembed" allowfullscreen="" width="660" height="371" frameborder="0"></iframe>

Ideally, I would like to retrieve the src from the beginning and just before the first question mark (?) as such:

http://www.youtube.com/embed/NM51qOpwcIM

Upvotes: 1

Views: 7813

Answers (1)

nozzleman
nozzleman

Reputation: 9649

This can be achieved using

(?<=src=").*?(?=[\?"])

See working example on Regex101

Explanation

  1. (?<=src=") Prepended by src="
  2. .*? Lazy match any token
  3. (?=[\?"]) Until either a ? or " would be the next token

If you might have a longer URL that doesn't end with ?

(?<=src=").*?(?=[\*"])

Upvotes: 10

Related Questions