1

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
1

1 Answer 1

10

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=").*?(?=[\*"])
Sign up to request clarification or add additional context in comments.

3 Comments

this does not working with ie11 browser. what will be solution that works across browsers ?
Works for me in IE11 too.
(?<=src=").*?(?=[*"]) No need to escape * inside the [] because we're specifying chars to filter inside the square brace.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.