简体   繁体   English

如何使用正则表达式或任何 javascript 方法从字符串中获取 url

[英]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.

相关问题 JavaScript:如何使用正则表达式从URL中提取字符串 - JavaScript: How to pull out a string from a URL using a regular expression 如何使用正则表达式验证javascript中的URL - how can i validate a url in javascript using regular expression 如何使用javascript正则表达式从字符串中获取域 - How to get domain from a string using javascript regular expression 如何从带有正则表达式的字符串中获取想要的部分 - How can I get wanted part from string with regular expression 如何在带有url的正则表达式字符串的javascript中查找? - How to find in javascript with regular expression string from url? 如何在Wordpress,PHP,JavaScript上使用正则表达式从字符串(URL)中删除特殊字符 - How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript 如何使用以下字符串中的正则表达式获取网址 - How to use the regular expression from the following string to get the url 从字符串中为JavaScript中的正则表达式获取子字符串 - Get a substring from a string for a regular expression in JavaScript 正则表达式从javascript中的字符串获取数字 - Regular expression to get numbers from the string in javascript 如何使用Javascript和正则表达式解析网址? - How to parse a url using Javascript and Regular Expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM