首页 > 解决方案 > 使用正则表达式抓取 url iframe 字符串

问题描述

细绳

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

我需要的是 src url 字符串。

我已经尝试了一种可以得到大部分内容的表达方式。

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

这返回

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

它返回了我不想要的最后的 \ 和 "。我尝试使用类似的东西

[^\"]

说“不要包括 \ 或”,但我没有做对。

正则表达式对我来说总是一个熊。

标签: javascriptregex

解决方案


请试试这个

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

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


推荐阅读