[英]How can i get the url from the string by using regular expression or any javascript method
[
"<iframe allowFullScreen frameborder=\"0\" height=\"564\" mozallowfullscreen src=\"https://player.vimeo.com/video/253266360\" webkitAllowFullScreen width=\"640\"></iframe>"
]
How can i get the below url from the above ptr string using reg exp: https://player.vimeo.com/video/253266360\ I am trying to split string but not able to get the url.我如何使用rem:Z5E056C56C56C500A1C4B6A7110B50B50D807Bade111111111111111111111111111111111111111111111111111111C411115E1111111115E1111115E1111115E1115E1111115E11115E11115E1115E11115E11115E1115E11115E15E115E1111115E111127BC115E8A027112Z从
ptr.split(/(?=:)/)
You can extract a URL from a string using the following RegExp:您可以使用以下 RegExp 从字符串中提取 URL:
/(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))/
So to get the URL from that string:因此,要从该字符串中获取 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];
When using RegExp.prototype.exec()
you, the first element in the returned array is the full string that was matched, in this case https://player.vimeo.com/video/253266360
.当您使用RegExp.prototype.exec()
时,返回数组中的第一个元素是匹配的完整字符串,在本例中https://player.vimeo.com/video/253266360
。 You can also remove the [0]
from the end of the url
expression to get the full returned array in case you need other information about the match.您还可以从url
表达式的末尾删除[0]
以获取完整的返回数组,以防您需要有关匹配的其他信息。
See MDN Docs for more details on the RegExp.exec
function.有关RegExp.exec
function 的更多详细信息,请参阅MDN 文档。
Perhaps you can leverage the DOM API and do like;也许您可以利用 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.