如何用正则表达式提取CSS中的图片路径?
展开全部
2009-12-22 13:19提问者: bbsboke |浏览次数:1158次比如str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg) repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; background:url(images/menu_homebj.jpg) repeat-x;}"
应该如何写正则表达式来提取“url(”与“)”中的图片地址???======== 满意答案 ========var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg) repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; background:url(images/menu_homebj.jpg) repeat-x;}"
var arr = str.match(/url\(.+?\)/ig);
for(var i =0; i<arr.length; i++){document.write(arr[i].replace("url(","").replace(")","") + "<br />");
}var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg)"+
"repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; "+
" background:url(images/menu_homebj.jpg) repeat-x;}";
var reg=/(?=url\()[^)]+(?=\))/g;
var r=str.match(reg),
result=[];for(var i=0;i<r.length;i++){
result.push(r[i].toString().slice(4,r[i].length));}alert(result);-----var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg) repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; background:url(images/menu_homebj.jpg) repeat-x;}";
var matches = [];
应该如何写正则表达式来提取“url(”与“)”中的图片地址???======== 满意答案 ========var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg) repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; background:url(images/menu_homebj.jpg) repeat-x;}"
var arr = str.match(/url\(.+?\)/ig);
for(var i =0; i<arr.length; i++){document.write(arr[i].replace("url(","").replace(")","") + "<br />");
}var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg)"+
"repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; "+
" background:url(images/menu_homebj.jpg) repeat-x;}";
var reg=/(?=url\()[^)]+(?=\))/g;
var r=str.match(reg),
result=[];for(var i=0;i<r.length;i++){
result.push(r[i].toString().slice(4,r[i].length));}alert(result);-----var str="NavBar{width:888px; height:35px; float:left;padding-left:62px; margin-top:1px; background:url(images/menu_bj.jpg) repeat-x; line-height:35px; font-size:12px;}.me{width:100px; height:35px; float:left; color:#FFFFFF; font-weight:bold; background:url(images/menu_homebj.jpg) repeat-x;}";
var matches = [];
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询