Javascript java脚本从字符串中提取链接

Javascript java脚本从字符串中提取链接,javascript,string,Javascript,String,我在字符串上有这样的代码: <html class=" no - js ie ie6 lt - ie9 lt - ie8 lt - ie7 search - context en_US " lang=" en - US "> <![endif]--><!--[if IE 7]> <html class=" no - js ie ie7 lt - ie9 lt - ie8 search - context en_US " lang=" en

我在字符串上有这样的代码:

<html class="
no - js ie ie6 lt - ie9 lt - ie8 lt - ie7 search - context en_US " lang="
en - US "> <![endif]--><!--[if IE 7]>         <html class="
no - js ie ie7 lt - ie9 lt - ie8 search - context en_US " lang="
en - US "> <![endif]--><!--[if IE 8]>         <html class="
no - js ie ie8 lt - ie9 search - context en_US " lang="
en - US "> <![endif]--><!--[if IE 9]>         <html class="
no - js ie9 search - context en_US " lang="
en - US "> <![endif]--><!--[if gt IE 9]><!--> <html class="
no - js search - context en_US " lang="
en - US "> <!--<![endif]-->    <head><title>art anime | Tumblr</title><!--[if ie]><meta http-equiv="
X - UA - Compatible " content="
IE = Edge, chrome = 1 "/><![endif]--><meta http-equiv="
Content - Type " content="
text / html;
charset = utf - 8 ">    <meta http-equiv="
x - dns - prefetch - control " content="
off ">    <meta name="
application - name " content="
Tumblr ">    <meta name="
msapplication - TileColor " content="
#3645d">    <meta name= "msapplication-TileImage"
content = "https://assets.tumblr.com/images/msfavicon.png?_v=245323c5cb69e705ea213d9ed60e543a" > < link rel = "shortcut icon"
href = "https://assets.tumblr.com/images/favicons/favicon.ico?_v=8bfa6dd3e1249cd567350c606f8574dc"
type = "image/png" > < meta name = "p:domain_verify"
content = "d06c4fa470a9a6935c9a7b43d57eb7d2" > < link rel = "apple-touch-icon"
sizes = "57x57"
href = "https://assets.tumblr.com/images/apple-touch-icon-57x57.png?_v=81406f92242ce0166328bc17e4473e6e"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "60x60"
href = "https://assets.tumblr.com/images/apple-touch-icon-60x60.png?_v=20e3957f7027b72d5aa60085204ae63c"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "72x72"
href = "https://assets.tumblr.com/images/apple-touch-icon-72x72.png?_v=8df24181ba31066d8710f67c9a287241"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "76x76"
href = "https://assets.tumblr.com/images/apple-touch-icon-76x76.png?_v=455617cae13eff40acffac5e489bde50"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "120x120"
href = "https://assets.tumblr.com/images/apple-touch-icon-120x120.png?_v=5604f95b165810101ea055f2cb5206b9"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "128x128"
href = "https://assets.tumblr.com/images/apple-touch-icon-128x128.png?_v=fd39307925fa7f1ada28b08d67e93da1"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "144x144"
href = "https://assets.tumblr.com/images/apple-touch-icon-144x144.png?_v=dfd5b392d423c5f0278d9f498abda2fa"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "152x152"
href = "https://assets.tumblr.com/images/apple-touch-icon-152x152.png?_v=73c2019bf6a75f7e476e01ba136cebec"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "180x180"
href = "https://assets.tumblr.com/images/apple-touch-icon-180x180.png?_v=00127c0342d97d5f36cfd8aa6439ca10"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "195x195"
href = "https://assets.tumblr.com/images/apple-touch-icon-195x194.png?_v=0"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "196x196"
href = "https://assets.tumblr.com/images/apple-touch-icon-196x196.png?_v=bb4b7eef0ef8e28101acdf2f0c265cc7"
type = "image/png" > < link rel = "apple-touch-icon"
sizes = "228x228"
href = "https://assets.tumblr.com/images/apple-touch-icon-228x228.png?_v=0c7874da12e347c2bdf95e5baa3f396a"
type = "image/png" > < link rel = "canonical"
href = "https://www.tumblr.com/search/art%20anime/recent" > < meta name = "robots"
id = "robots"
content = "noodp,noydir" > < meta name = "description"
id = "description"
content = "Tumblr is a place to express yourself, discover yourself, and bond over the stuff you love. It&#039;s where your interests connect you with your people." > < meta name = "keywords"
id = "keywords"
content = "tumblelog, blog, tumblog, tumbler, tumblr, tlog, microblog" > < meta name = "viewport"
id = "viewport"
content = "width=960" > < meta name = "tumblr-form-key"
id = "tumblr_form_key"
content = "!1231553203788|mOZjoWSzPU6eNRkpuhEtzoBJmdA" > < meta name = "tumblr-gpop"
id = "tumblr_gpop"
content = "Tumblr" > < meta name = "og:title"
id = "og_title"
content = "art anime | Tumblr" > < meta name = "og:image"
id = "og_image"
content = "https://66.media.tumblr.com/06afadc2cafb6945065a4b10d61f3b45/tumblr_poqjiuUUGO1tvlw71o1_r1_500.jpg" > < script type = "application/ld+json" >

您可以使用诸如“is=”og_image“content=“(^“*)”之类的正则表达式,它将接受每个内容url
如果只需要最后一个,还可以添加id参数验证

您可以使用正则表达式,如'is=“og_image”content=“(^“*)”,它将接受每个内容url
如果只需要最后一个,还可以添加id参数验证

使用
jquery
获取它。根据上面的示例,有一个唯一的
og_image
id
,因此使用它来获取
meta
元素,然后将其缩小到
content
属性

$("meta #og_image").attr("content");
假设在一个变量myString中已经有了整个混乱:

var regex = /66\.[^"]*/;
var myLink = myString.match(regex);
myLink[0]将是url

为了使此答案对其他类似情况(而不仅仅是这种特殊情况)更有用,如果您试图从包含多个链接的长字符串中提取链接,您可以使用:

var regex = /http[^"]*/g;
var links = myString.match(regex);
这将为您提供一个包含所有链接的数组

links[links.length-1];

将是最后一个,这是您在本例中想要的。

使用
jquery
获取它。根据上面的示例,有一个唯一的
og_image
id
,因此使用它来获取
meta
元素,然后将其缩小到
content
属性

$("meta #og_image").attr("content");
假设在一个变量myString中已经有了整个混乱:

var regex = /66\.[^"]*/;
var myLink = myString.match(regex);
myLink[0]将是url

为了使此答案对其他类似情况(而不仅仅是这种特殊情况)更有用,如果您试图从包含多个链接的长字符串中提取链接,您可以使用:

var regex = /http[^"]*/g;
var links = myString.match(regex);
这将为您提供一个包含所有链接的数组

links[links.length-1];


将是最后一个,这是您在本例中想要的。

这是您想要解析的精确字符串?是的,我如何精确?这个“字符串”看起来不像普通字符串-更像html-并且不能真正用作所有这些字符串“里面的人物。你从哪里得到它?你是如何存储这个sting内容的,当你把它放入任何变量时,它会出错,这就是你想要解析的确切字符串?是的,我是如何精确的?这个“字符串”看起来不像一个普通的字符串-更像html-并且不能作为一个字符串与所有这些字符串一起使用“里面的人物。你从哪里得到的?你是如何存储这些内容的,当你把它放入任何变量时,它会出错,我不知道如何添加参数验证,你能举个例子吗?我更新了我的代码。如果您需要更多详细信息,我可以稍后制作整个代码示例jQuery示例——您只需要用字符串初始化对象。和创建要添加到页面的新html元素的方法相同。我不知道如何添加参数验证,你能举个例子吗?我已经更新了代码。如果您需要更多详细信息,我可以稍后制作整个代码示例jQuery示例——您只需要用字符串初始化对象。与创建要添加到页面的新html元素的方法相同。但问题是,如果变量中已经包含此“字符串”,则添加了javascript正则表达式以解析此“字符串”中的url。但问题是,添加了javascript正则表达式以解析此“字符串”中的url如果变量中已经有它。