0

String

let iframe = "<iframe src=\"https://player.vimeo.com/video/111111\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen></iframe>";

What I need is the src url string.

I've tried an expression that gets most of it.

let url = iframe.match(/\bhttps?:\/\/\S+);

This returns

'https://player.vimeo.com/video/111111\"'

It's returning the final \ and " which I don't want. I've tried to use something like

[^\"]

to say "don't include the \ or " but I'm not getting it right.

Regex is always a bear for me.

flimflam57
  • 1,334
  • 4
  • 16
  • 31

2 Answers2

1

Can use DOM methods by inserting the html into an empty container element and getting the src property of the iframe element

let iframe = "<iframe src=\"https://player.vimeo.com/video/111111\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen></iframe>";

let div = document.createElement('div')
div.innerHTML = iframe

let url = div.querySelector('iframe').src

console.log(url)
charlietfl
  • 170,828
  • 13
  • 121
  • 150
1

Please try this

let iframe = "<iframe src=\"https://player.vimeo.com/video/111111\" width=\"500\" height=\"281\" frameborder=\"0\" allowfullscreen></iframe>";

console.log(iframe.match('(?<=src=").*?(?=[\?"])'));