Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用正则表达式从字符串中提取图像url_Javascript_Regex_String Parsing - Fatal编程技术网

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

Javascript 使用正则表达式从字符串中提取图像url,javascript,regex,string-parsing,Javascript,Regex,String Parsing,我有一个字符串如下所示: 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 /&

我有一个字符串如下所示:

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)=(\\"[^"]*\")/i;
var m;
do {
  m = re.exec(complicatedString);
} while(m !== null);

更新:Regex 101声称它可以工作

问题在于Regex

字符串中的反斜杠用于转义双引号字符串中的双引号反斜杠是转义字符,不是字符串的一部分。因此,在正则表达式中不需要这些字符

使用

此处需要
g
标志,因为正则表达式的
lastIndex
属性没有更新,下一次迭代正则表达式将从同一索引开始搜索,因此将进入无限循环。

var compositedstring=“\n当前情况:\n
晴朗\n
\n
\n预测:\n
周五晴朗。高:23低:13\n
周六雷暴。高:25低:15\n
太阳雷暴。高:28低:21\n
周一部分多云。高:24低:17\n
周二部分多云。高:26低:18\n
\n(由提供)\n
\n]]>”; 变量re=/(alt | title | src)=(“[^”]*”)/gi; var-m; while(m=re.exec(复杂化字符串)){ console.log(m[2]);
}
问题在于正则表达式

字符串中的反斜杠用于转义双引号字符串中的双引号。反斜杠是转义字符,而不是字符串的一部分。因此,在正则表达式中不需要这些字符

使用

此处需要
g
标志,因为正则表达式的
lastIndex
属性没有更新,下一次迭代正则表达式将从同一索引开始搜索,因此将进入无限循环。

var compositedstring=“\n当前情况:\n
晴朗\n
\n
\n预测:\n
周五晴朗。高:23低:13\n
周六雷暴。高:25低:15\n
太阳雷暴。高:28低:21\n
周一部分多云。高:24低:17\n
周二部分多云。高:26低:18\n
\n(由提供)\n
\n]]>”; 变量re=/(alt | title | src)=(“[^”]*”)/gi; var-m; while(m=re.exec(复杂化字符串)){ console.log(m[2]);
}
这里有一个对您的输入有效的可能幼稚的正则表达式:
/(https?:\/\/.\.\(?:png | jpg | gif))/

这里有一个可能对您的输入有用的简单正则表达式:
/(https?:\/\/.\(?:png | jpg | gif))/

这听起来像是XY问题,可能不需要正则表达式

您使用的是Yahoo!Weather API。这些图像的代码直接在API中提供,因此不需要解析结果来查找图像

下面是一个示例API终结点:**%20from%20weather.forecast%20where%20woeid%20in%20(从%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22中选择%20woeid%20)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

不同的状态图像保存为
code
属性。您可以直接访问该图像代码。以下示例提取图像代码、日期和天气描述

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();
var-apirl=”https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(从%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22中选择%20woeid%20)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys”
功能附件本体(obj){
document.getElementsByTagName(“正文”)[0].innerHTML+=(“+obj.date+”:“+obj.text+”);
}
函数loadDoc(){
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
var response=JSON.parse(xhttp.responseText);
var current=response.query.results.channel.item.condition;
附体(电流);
var forecast=response.query.results.channel.item.forecast;
对于(变量i=0;i
示例输出:

2016年6月10日星期五上午6:00 AKDT:多云
2016年6月10日:多云
2016年6月11日:多云
2016年6月12日:多云
2016年6月13日:雨
2016年6月14日:多云
2016年6月15日:部分多云
2016年6月16日:多云
2016年6月17日:零星阵雨
2016年6月18日:多云
2016年6月19日:多云

看到它在这个JS小提琴中工作了吗

此处提供的天气API文档如下:


希望有帮助!

这听起来像是XY问题,可能不需要正则表达式

您使用的是Yahoo!Weather API。这些图像的代码直接在API中提供,因此不需要解析结果来查找图像

下面是一个示例API终结点:**%20from%20weather.forecast%20where%20woeid%20in%20(从%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22中选择%20woeid%20)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys

不同的状态图像保存为
code
属性。您可以直接访问该图像代码。以下示例提取图像代码、日期和天气描述

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();
var-apirl=”https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(从%20geo.places(1)%20where%20text%3D%22nome%2C%20ak%22中选择%20woeid%20)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys”
功能附件本体(obj){
document.getElementsByTagName(“正文”)[0].innerHTML+=(“+obj.date+”:“+obj.text+”);
}
函数loadDoc(){
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
var response=JSON.parse(xhttp.responseText);
var current=response.query.results.channel.item.condition;
附体(电流);
var forecast=response.query.results.channel.item.forecast;
对于(变量i=0;i<0)
/(alt|title|src)=("[^"]*")/gi;
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();