Home > OS >  fetch src value from the string
fetch src value from the string

Time:09-21

correct regex to fetch src value from the string.

 var str = "<iframe width=\"1000\" height=\"460\" src=\"https://myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z\" title=\"Test Video for New Component\"></iframe>";
    
 var re = /^src/g;
 var n = str.match(re);
 console.log(n);

//output &quot;https://myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z&quot; title=&quot;Test Video for New Component&quot;></iframe>

CodePudding user response:

We can try using match with the regex pattern \bsrc=\S :

var str = "&lt;iframe width=\&quot;1000\&quot; height=\&quot;460\&quot; src=\&quot;https&#58;//myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z\&quot; title=\&quot;Test Video for New Component\&quot;&gt;&lt;/iframe&gt;";
var src = str.match(/\bsrc=\S /);
console.log(src);

CodePudding user response:

If you know your input will always look like this or similar, you can also just use replace:

const str = "&lt;iframe width=\&quot;1000\&quot; height=\&quot;460\&quot; src=\&quot;https&#58;//myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z\&quot; title=\&quot;Test Video for New Component\&quot;&gt;&lt;/iframe&gt;";

const src = str.replace(/^.*?src=\&quot;(.*?)\&quot;.*?$/, "$1");

console.log(src);

  • Related