I have this string i need to extract the url from this.
<figure class="media"><oembed url="https://www.youtube.com/watch?v=P******"></oembed></figure>
I need to get output like this
url= https://www.youtube.com/watch?v=P******
I have this string i need to extract the url from this.
<figure class="media"><oembed url="https://www.youtube.com/watch?v=P******"></oembed></figure>
I need to get output like this
url= https://www.youtube.com/watch?v=P******
Beware Zalgo. Use a proper HTML parser where available, instead of regexp.
const source = '<figure class="media"><oembed url="https://www.youtube.com/watch?v=P******"></oembed></figure>';
const div = document.createElement('div');
div.innerHTML = source;
const oembed = div.querySelector('oembed');
console.log(oembed.getAttribute('url'));
You can use regular expression to fetch the value.
var str = `<figure class="media"><oembed url="https://www.youtube.com/watch?v=P******"></oembed></figure>`;
console.log(str.match(/url=\".*?\"/))
This is pretty straightforward
let url = document.querySelector('figure.media oembed').getAttribute('url');
console.log(url);
// gives: https://www.youtube.com/watch?v=P******
You can match URL using match operation through regex
const string = `<figure class="media">
<oembed url="https://www.youtube.com/watch?v=P******"></oembed>
</figure>`
const matches = string.match(/\bhttps?:\/\/.+(?=")/gi)
// ["https://www.youtube.com/watch?v=P******"]
console.log({ url: matches[0] })
// https://www.youtube.com/watch?v=P******
let str1 = '<figure class="media"><oembed url="https://www.youtube.com/watch?v=P******"></oembed></figure>';
let urlIndex = str1.indexOf("url=");
let extractUrl = str1.substring(urlIndex + 5, str1.indexOf('">', urlIndex));