新人问个小问题~~求助~
<img src="/Magazine/attached/image/20130226/20130226135413_594.jpg" alt="" />
<img src="/Magazine/attached/image/20130226/20130226135413_595.jpg" alt="" />
<img src="/Magazine/attached/image/20130226/20130226135413_596.jpg" alt="" />
怎么得到SRC后面的值?并赋给一个字符串?
最后结果可以得到String conten = "/Magazine/attached/image/20130226/20130226135413_594.jpg,/Magazine/attached/image/20130226/20130226135413_595.jpg,/Magazine/attached/image/20130226/20130226135413_596.jpg"
------解决方案--------------------用js中的getElementByTagName
如:var imgs = document.getElementByTagName("img");
得到的是img的数组,然后循环遍历,取出src属性的值进行拼接
如:imgs[i].getAttribute("src")
------解决方案--------------------
public static List<String> match(String source, String element, String attr) {
List<String> result = new ArrayList<String>();
String reg = "<" + element + "[^<>]*?\\s" + attr + "=['\"]?(.*?)['\"]?\\s.*?>";
Matcher m = Pattern.compile(reg).matcher(source);
while (m.find()) {
String r = m.group(1);
result.add(r);
}
return result;
}
public static void main(String[] args) {
String source = "<img src='/Magazine/attached/image/20130226/20130226135413_594.jpg' alt='' />";
List<String> list = match(source, "img", "src");
System.out.println(list.get(0));
}