Javascript Regex将用文本形式的iframes src中的youtube链接替换iframe
我有一个文本,其中可能包含多个Javascript Regex将用文本形式的iframes src中的youtube链接替换iframe,javascript,jquery,regex,Javascript,Jquery,Regex,我有一个文本,其中可能包含多个iframe,我打算从该iframe中获取src,并用该src中的文本替换整个iframe 举个例子,如果我有: Some text here. After that, an iframe: <iframe src="//www.youtube.com/embed/GplWFSU3tLA"></iframe> Some text here. After that, an iframe: //www.youtube.com/embed/Gpl
iframe
,我打算从该iframe中获取src,并用该src中的文本替换整个iframe
举个例子,如果我有:
Some text here. After that, an iframe: <iframe src="//www.youtube.com/embed/GplWFSU3tLA"></iframe>
Some text here. After that, an iframe: //www.youtube.com/embed/GplWFSU3tLA
这就是我尝试做的:我为iframe编写了一个regexp,为youtube链接编写了一个regexp。然后我获取我想要解析的文本,我使用.replace
替换所有iframe,并在函数中传递匹配iframe的x
,然后运行youtube regexp仅查找youtube链接,然后返回链接(理论上应该替换匹配的iframe)
我怀疑我要么编写了一个或两个RegExp
,要么我使用了。替换的方法不正确。谢谢你的见解
$(文档).ready(函数(){
var iframeHunter=new RegExp(/您始终可以在iframe上设置一个ID
在JSFIDLE集合中,将加载类型设置为无换行-底部的
const theIFrame=document.getElementById(“某些id”)
document.getElementById(“replacedOutput”).textContent=theIFrame.src;
否则使用regEx
(www.youtube.com\/embed\/)(\w+)
忽略下面
[0]完全匹配www.youtube.com/embed/GplWFSU3tLA
[1] 第一组www.youtube.com/embed/
[2] 第2组GplWFSU3tLA
您好!谢谢您的回答!我应该如何引用第2组,以便将其返回到iframe的位置?
<p id="textHolder">Som
e text here. After that, an iframe: <iframe id="some-id" width="420" height="315" src="//www.youtube.com/embed/GplWFSU3tLA" frameborder="0" allowfullscreen></iframe>
</p>
const textHolder = document.getElementById("textHolder").innerHTML;
// Remove g to find first match only
const regEx = /(www.youtube.com\/embed\/)(\w+)/g
const match = textHolder.match(regEx);
// Note match will return an array for every match it finds.
// [0] www.youtube.com/embed/GplWFSU3tLA
console.log(match);