我需要一个正则表达式的帮助,我正在通过ajax读取一个外部URL,其中包含以下代码:
<body>
originalAdvertiserLink='http://normaldomain.com/7cc86d4999/'<div id='beacon_d14503cb95' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'><img src='http://URL.com/www/delivery/lg.php?bannerid=451&campaignid=91&zoneid=19&loc=1&cb=d14503cb95' width='0' height='0' alt='' style='width: 0px; height: 0px;' /></div>
</body>
我需要在originalAdvertiserLink =''中提取我已使用此代码执行的URL:
var e = /originalAdvertiserLink=\'([^\']*)/.exec(data);
openxOriginalLink = e[1];
但我还需要提取所有div标签,我的意思是这部分:
<div id='beacon_d14503cb95' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'><img src='http://URL.com/delivery/lg.php?bannerid=451&campaignid=91&zoneid=19&loc=1&cb=d14503cb95' width='0' height='0' alt='' style='width: 0px; height: 0px;' /></div>
如何编写正则表达式以仅提取该部分,就像我使用上面的URL一样?
非常感谢
答案 0 :(得分:0)
有些事情(如果你不想改变目前的做事方式 - 评论不推荐):
var body = "<body>originalAdvertiserLink='http://normaldomain.com/7cc86d4999/'<div id='beacon_d14503cb95' style='position: absolute; left: 0px; top: 0px; visibility: hidden;'><img src='http://URL.com/www/delivery/lg.php?bannerid=451&campaignid=91&zoneid=19&loc=1&cb=d14503cb95' width='0' height='0' alt='' style='width: 0px; height: 0px;' /></div></body>";
var regex = /<div(.*?)div>/ig;
var match = regex.exec(body);
if(match.length > 0)
{
match = match[0];
alert(match);
}
JSFiddle:http://jsfiddle.net/56dKY/