[
"<iframe allowFullScreen frameborder=\"0\" height=\"564\" mozallowfullscreen src=\"https://player.vimeo.com/video/253266360\" webkitAllowFullScreen width=\"640\"></iframe>"
]
我如何使用 reg exp 從上面的 ptr 字串獲取下面的 url: https://player.vimeo.com/video/253266360\ 我正在嘗試拆分字串但無法獲取 url。
ptr.split(/(?=:)/)
uj5u.com熱心網友回復:
您可以使用以下 RegExp 從字串中提取 URL:
/(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\ ~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\ .~#?&//=]*))/
因此,要從該字串中獲取 URL:
const ptr = '<iframe allowFullScreen frameborder="0" height="564" mozallowfullscreen src="https://player.vimeo.com/video/253266360" webkitAllowFullScreen width="640"></iframe>'
const url = /(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\ ~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\ .~#?&//=]*))/.exec(ptr)[0];
使用RegExp.prototype.exec()您時,回傳陣列中的第一個元素是匹配的完整字串,在本例中為https://player.vimeo.com/video/253266360。您還可以[0]從url運算式的末尾洗掉以獲取完整的回傳陣列,以防您需要有關匹配的其他資訊。
有關該功能的更多詳細資訊,請參閱MDN 檔案RegExp.exec。
uj5u.com熱心網友回復:
也許您可以利用 DOM API 并這樣做;
var div = document.createElement("div"),
src;
div.innerHTML = "<iframe allowFullScreen frameborder=\"0\" height=\"564\" mozallowfullscreen src=\"https://player.vimeo.com/video/253266360\" webkitAllowFullScreen width=\"640\"></iframe>";
src = div.firstChild.src;
console.log(src);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/373414.html
標籤:javascript 正则表达式 细绳 分裂
