简体   繁体   English

使用正则表达式从字符串中提取图像URL

[英]Using regex to extract an image url from string

I have a string that looks like this: 我有一个看起来像这样的字符串:

var complicatedString = "<![CDATA[<img src=\"http://l.yimg.com/a/i/us/we/52/32.gif\"/>\n<BR />\n<b>Current Conditions:</b>\n<BR />Sunny\n<BR />\n<BR />\n<b>Forecast:</b>\n<BR /> Fri - Sunny. High: 23Low: 13\n<BR /> Sat - Thunderstorms. High: 25Low: 15\n<BR /> Sun - Thunderstorms. High: 28Low: 21\n<BR /> Mon - Partly Cloudy. High: 24Low: 17\n<BR /> Tue - Partly Cloudy. High: 26Low: 18\n<BR />\n<BR />\n<a href=\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-23511893/\">Full Forecast at Yahoo! Weather</a>\n<BR />\n<BR />\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)\n<BR />\n]]>"

I need to extract http://l.yimg.com/a/i/us/we/52/32.gif . 我需要提取http://l.yimg.com/a/i/us/we/52/32.gif The regex I came up with is: 我想出的正则表达式是:

var re = /(alt|title|src)=(\\"[^"]*\")/i;

See Fiddle: https://jsfiddle.net/47rveu62/2/ 见小提琴: https//jsfiddle.net/47rveu62/2/

I'm not sure why but this isn't working. 我不确定为什么,但这不起作用。

var re = /(alt|title|src)=(\\"[^"]*\")/i;
var m;
do {
  m = re.exec(complicatedString);
} while(m !== null);

Update: Regex 101 claims it works https://regex101.com/r/oV2hO2/1 更新: Regex 101声称它可以运行https://regex101.com/r/oV2hO2/1

The problem is with the regex. 问题在于正则表达式。

The backslashes in the string are used to escape the double-quote inside double-quoted string. 字符串中的反斜杠用于转义双引号字符串中的双引号。 The backslashes are the escape characters and not part of the string. 反斜杠是转义字符,不是字符串的一部分。 So, in regex those are not required. 因此,在正则表达式中,这些不是必需的。

以下是在登录控制台时字符串的外观

var re = /(alt|title|src)=(\\"[^"]*\")/i;
                           ^^      ^     // Remove those

Use 采用

/(alt|title|src)=("[^"]*")/gi;

The g flag here is required as the lastIndex property of the regex is not updated by RegExp#exec and the next iteration the regex will start search from the same index and will thus go in infinite loop. 这里的g标志是必需的,因为正则表达式的lastIndex属性不会被RegExp#exec更新,并且下一次迭代正则表达式将从相同的索引开始搜索,因此将进入无限循环。 MDN MDN

 var complicatedString = "<![CDATA[<img src=\\"http://l.yimg.com/a/i/us/we/52/32.gif\\"/>\\n<BR />\\n<b>Current Conditions:</b>\\n<BR />Sunny\\n<BR />\\n<BR />\\n<b>Forecast:</b>\\n<BR /> Fri - Sunny. High: 23Low: 13\\n<BR /> Sat - Thunderstorms. High: 25Low: 15\\n<BR /> Sun - Thunderstorms. High: 28Low: 21\\n<BR /> Mon - Partly Cloudy. High: 24Low: 17\\n<BR /> Tue - Partly Cloudy. High: 26Low: 18\\n<BR />\\n<BR />\\n<a href=\\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-23511893/\\">Full Forecast at Yahoo! Weather</a>\\n<BR />\\n<BR />\\n(provided by <a href=\\"http://www.weather.com\\" >The Weather Channel</a>)\\n<BR />\\n]]>"; var re = /(alt|title|src)=("[^"]*")/gi; var m; while(m = re.exec(complicatedString)) { console.log(m[2]); } 


I'd suggest you to use following regex 我建议你使用以下正则表达式

/img.*?src=("|')(.*?)\1/i;

 var complicatedString = "<![CDATA[<img src=\\"http://l.yimg.com/a/i/us/we/52/32.gif\\"/>\\n<BR />\\n<b>Current Conditions:</b>\\n<BR />Sunny\\n<BR />\\n<BR />\\n<b>Forecast:</b>\\n<BR /> Fri - Sunny. High: 23Low: 13\\n<BR /> Sat - Thunderstorms. High: 25Low: 15\\n<BR /> Sun - Thunderstorms. High: 28Low: 21\\n<BR /> Mon - Partly Cloudy. High: 24Low: 17\\n<BR /> Tue - Partly Cloudy. High: 26Low: 18\\n<BR />\\n<BR />\\n<a href=\\"http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-23511893/\\">Full Forecast at Yahoo! Weather</a>\\n<BR />\\n<BR />\\n(provided by <a href=\\"http://www.weather.com\\" >The Weather Channel</a>)\\n<BR />\\n]]>"; var regex = /img.*?src=("|')(.*?)\\1/i; var match = complicatedString.match(regex)[2]; console.log(match); 

这是一个可能天真的正则表达式,适用于您的输入:/( /(https?:\\/\\/.*\\.(?:png|jpg|gif))/ : /(https?:\\/\\/.*\\.(?:png|jpg|gif))/

This sounds like an XY problem, and regex may not be necessary. 这听起来像是一个XY问题,并且可能没有必要使用正则表达式。

You're using the Yahoo! 您正在使用Yahoo! Weather API. 天气API。 The codes for the images are available directly in the API, so there is no need to parse the result to find the image. 图像的代码可直接在API中使用,因此无需解析结果以查找图像。

Here is an example API endpoint: https://query.yahooapis.com/v1/public/yql?q=select%20 *%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys 以下是一个示例API端点: https ://query.yahooapis.com/v1/public/yql?q = select%20 *%20from%20weather.forecast%20where%20woeid%20in%20(选择%20woeid%20from% 20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&格式= JSON&ENV =商店%3A%2F%2Fdatatables.org%2Falltableswithkeys

The different status images are saved as the code property. 不同的状态图像保存为code属性。 You can access that image code directly. 您可以直接访问该图像代码。 Here is an example that pulls the image code, date, and weather description. 这是一个拉动图像代码,日期和天气描述的示例。

var apiUrl = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"

function appendToBody(obj) {
    document.getElementsByTagName("body")[0].innerHTML += ("<div><img src='http://l.yimg.com/a/i/us/we/52/"+obj.code+".gif' />" + obj.date +": "+obj.text+"</div>");
}


function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
     var response = JSON.parse(xhttp.responseText);
     var current = response.query.results.channel.item.condition;
     appendToBody(current);



     var forecast = response.query.results.channel.item.forecast;

     for(var i = 0; i < forecast.length; i++) {
            appendToBody(forecast[i]);
     }
    }
  };
  xhttp.open("GET", apiUrl, true);
  xhttp.send();
}


loadDoc();

Example output: 示例输出:

<div><img src="http://l.yimg.com/a/i/us/we/52/26.gif">Fri, 10 Jun 2016 06:00 AM AKDT: Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/26.gif”>周五,2016年6月10日上午06:00 AKDT:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/26.gif">10 Jun 2016: Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/26.gif”> 2016年6月10日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/28.gif">11 Jun 2016: Mostly Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/28.gif”> 2016年6月11日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/26.gif">12 Jun 2016: Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/26.gif”> 2016年6月12日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/12.gif">13 Jun 2016: Rain</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/12.gif”> 2016年6月13日:Rain </ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/28.gif">14 Jun 2016: Mostly Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/28.gif”> 2016年6月14日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/30.gif">15 Jun 2016: Partly Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/30.gif”> 2016年6月15日:部分多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/26.gif">16 Jun 2016: Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/26.gif”> 2016年6月16日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/39.gif">17 Jun 2016: Scattered Showers</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/39.gif”> 2016年6月17日:零星阵雨</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/26.gif">18 Jun 2016: Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/26.gif”> 2016年6月18日:多云</ div>
<div><img src="http://l.yimg.com/a/i/us/we/52/28.gif">19 Jun 2016: Mostly Cloudy</div> <div> <img src =“http://l.yimg.com/a/i/us/we/52/28.gif”> 2016年6月19日:多云</ div>

See it working in this JS Fiddle https://jsfiddle.net/igor_9000/u27br5Lg/2/ 看到它工作在这个JS小提琴https://jsfiddle.net/igor_9000/u27br5Lg/2/

The weather API documentation here available here: https://developer.yahoo.com/weather/documentation.html 此处提供了天气API文档: https//developer.yahoo.com/weather/documentation.html

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM