我想使用javascript regexp從html代碼中選擇圖像源url。我正在使用它來簡化在其他網站中使用picasaweb圖像。我在這方面頗爲新穎,我使用http://www.regular-expressions.info/javascriptexample.html構建了一個正則表達式,在那裏它像一個魅力一樣工作,但不在我自己的腳本中。有人能指出我腳本中的錯誤嗎?使用javascript正則表達式從html中選擇圖像url
function addImage() {
var picasaDump=prompt("Copy the picasa link");
if (picasaDump!=null && picasaDump!="")
{
var regMatch = new RegExp("http:\/\/\S\.[jJ][pP][eE]?[gG]");
var imageLink = regMatch.exec(picasaDump);
if(imageLink == null) {
alert("Error, no images found");
} else if(imageLink.length > 1) {
alert("Error, multiple images fount");
} else {
further parsing...
}
}
}
編輯:一些樣品輸入
<a href="http://picasaweb.google.com/lh/photo/NHH78Y0WLPAAzIu0lzKlUA?feat=embedwebsite"><img src="http://lh3.ggpht.com/_ADW_3zOQhj8/TGgN4bXtfMI/AAAAAAAABCA/w6M-JKzNtBk/s144/DSC_2132.jpg" /></a>
太感謝了,這就是我正在尋找的解決方案! – TuxM 2010-08-17 13:51:46