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 "https://myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z" title="Test Video for New Component"></iframe>
CodePudding user response:
We can try using match
with the regex pattern \bsrc=\S
:
var str = "<iframe width=\"1000\" height=\"460\" src=\"https://myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z\" title=\"Test Video for New Component\"></iframe>";
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 = "<iframe width=\"1000\" height=\"460\" src=\"https://myvideo.dpdhl.com/video/CuEt8XJ8uVEU3MZrrjQp5z\" title=\"Test Video for New Component\"></iframe>";
const src = str.replace(/^.*?src=\"(.*?)\".*?$/, "$1");
console.log(src);