我正在尝试使用 javascript 的正则表达式提取 img
的 src
但我无法使用这些组。到目前为止,我想出了这样的事情:
>str
"xxxxx src="http://www.omgubuntu.co.uk/wp-content/uploads/2013/12/Multim.jpg" xxxxx"
>str.match(re)
["src="http://www.omgubuntu.co.uk/wp-content/uploads/2013/12/Multim.jpg""]
>re
/src=\"(.*?)\"/g
>str.match(re)[1]
undefined
但我只得到了整个模式的匹配
最佳答案
不确定为什么要使用正则表达式执行此操作;
只需使用:
document.getElementById('idOfImgElement').src;
或者更简单,使用 jQuery:
$('#idOfImgElement').attr(src);
但是如果你真的想用正则表达式做这个,使用:
var str = "<img src=\"http://www.omgubuntu.co.uk/wp-content/uploads/2013/12/Multim.jpg\" />";
var matches = (/src=\"(.*?)\"/g).exec(str);
window.print(matches[1]);
关于javascript - 在javascript中提取正则表达式组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20447310/