求一个正则用于匹配body标签的 抓取的html文本我想用正则匹配出body标签内的所有内容,这个该怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /<body>.*<\/body>/??不是很明白楼主的意思 就是要body内的全部内容呀,你这个不行呀,开始我是这样写的,你试下就知道了 正则是用来匹配字符串和文本的 你的网页的内容是保存成文本了?还是说你想通过js获取这个网页的body里的内容 保存成文本了,你那个正则只有body标签内的东西都在同一行时才能匹配到,一有换行就不行 字符串的话可以用var bodyHTML;try{var htmlStr = eval('('+str+')');var bodyHTML = htmlStr.body.innerHTML;}catch(e){ } 你应该是把网页保存后用某个编辑软件打开这个网页后搜索吧?我用EmEditor打开后搜索时在自定义里边吧.匹配换行符选中和.匹配换行符的行数改大后用那个正则匹配可以的要不版主的那个匹配字符串可以 但在编辑器里边不行 结果如下------------------------------------------------结果是<body><a>12312312312312</a></body>可以匹配的到<body> <a>12312312312312</a></body>无法匹配------------------------------------------------ <html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body> <a>12312312312312</a></body></html>这种没问题。稍微复杂一点就匹配不到如:\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>\r\n\r\n</title>\r\n <script type=\"text/javascript\" src=\"js/jquery-1.6.2.js\"></script>\r\n\t<script type=\"text/javascript\" src=\"js/custom.js\"></script>\r\n\r\n\t<link href=\"css/ui/ui.base.css\" rel=\"stylesheet\" media=\"all\" /><link href=\"css/themes/gray_standard/ui.css\" rel=\"stylesheet\" title=\"style\" media=\"all\" />\r\n\r\n\t<!--[if IE 6]>\r\n\t<link href=\"css/ie6.css\" rel=\"stylesheet\" media=\"all\" />\r\n\t<script src=\"js/pngfix.js\"></script>\r\n\t<script>\r\n\t /* Fix IE6 Transparent PNG */\r\n\t DD_belatedPNG.fix(\'.logo, ul#dashboard-buttons li a, .response-msg, #search-bar input\');\r\n\t</script>\r\n\t<![endif]-->\r\n \r\n</head>\r\n<body onunload=\"setheight()\">\r\n <form method=\"post\" action=\"welcome.aspx?action=get&name=\" id=\"form1\">\r\n<div class=\"aspNetHidden\">\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/wEPDwUKMTY1NDU2MTA1MmRkj69cULYw1yCwNSQyt8QDH6tFWzo6ZRbIVECBzNiRyAY=\" />\r\n</div>\r\n\r\n <div>\r\n \r\n <div class=\"inner-page-title\">\r\n <h2>Welcome to Admintasia 2.3 Live Demonstration</h2>\r\n\t\t\t\t\t <span>You can start building your next user interface with this powerful UI framework !</span>\r\n\t\t\t\t </div>\r\n <div class=\"clear\"></div>\r\n\t\t\t\t <div class=\"content-box\">\r\n\t\t\t\t\t <div class=\"two-column\">\r\n\t\t\t\t\t\t <div class=\"column\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n <div class=\"portlet-header ui-widget-header\">New Release: Admintasia 2.3<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\r\n\t\t\t\t\t\t\t\t\t <p>\r\n\t\t\t\t\t\t\t\t\t\t <a href=\"http://www.admintasia.com\">\r\n <b>Visit the presentation website</b>\r\n </a> | <b><a href=\"http://www.admintasia.com/live-demo\"><b>View Live Demonstration</b></a></b>\r\n\t\t\t\t\t\t\t\t\t\t <br /><br />\r\n Prices start from <b>$50</b> for the <b>Regular License</b> \r\n and <b>$299</b> for the <b>Extended License</b>. Both licenses \r\n come with <b>support access</b> and <b>lifetime updates</b>. \r\n </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t <div class=\"column column-right\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-header ui-widget-header\">Affiliate Program<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\t\t\t\t\t\t\t\t\t\r\n <p> Join our affiliates program and earn 51% of every sale. \r\n <br />\r\n <br /><b><a href=\"http://www.admintasia.com/affiliates/\">Click here for more details about our affiliates program</a></b>.\r\n\t\t\t\t\t\t\t\t\t </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t </div>\r\n\t\t\t\t\t ............................<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n \r\n\r\n </div>\r\n </form>\r\n <script type=\"text/javascript\">\r\n $(document).ready(function () {\r\n parent.setheight(document.body.clientHeight);\r\n });\r\n function setheight() {\r\n parent.setheight(814);\r\n }\r\n </script>\r\n</body>\r\n</html>\r\n"复杂点上面就匹配不到了。 /<body.*?>([\s\S]*?)<\/body>/ /<body[\s\S]*?>([\s\S]*?)<\/body>/ var s="\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>\r\n\r\n</title>\r\n <script type=\"text/javascript\" src=\"js/jquery-1.6.2.js\"><\/script>\r\n\t<script type=\"text/javascript\" src=\"js/custom.js\"><\/script>\r\n\r\n\t<link href=\"css/ui/ui.base.css\" rel=\"stylesheet\" media=\"all\" /><link href=\"css/themes/gray_standard/ui.css\" rel=\"stylesheet\" title=\"style\" media=\"all\" />\r\n\r\n\t<!--[if IE 6]>\r\n\t<link href=\"css/ie6.css\" rel=\"stylesheet\" media=\"all\" />\r\n\t<script src=\"js/pngfix.js\"><\/script>\r\n\t<script>\r\n\t /* Fix IE6 Transparent PNG */\r\n\t DD_belatedPNG.fix(\'.logo, ul#dashboard-buttons li a, .response-msg, #search-bar input\');\r\n\t<\/script>\r\n\t<![endif]-->\r\n \r\n</head>\r\n<body onunload=\"setheight()\">\r\n <form method=\"post\" action=\"welcome.aspx?action=get&name=\" id=\"form1\">\r\n<div class=\"aspNetHidden\">\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/wEPDwUKMTY1NDU2MTA1MmRkj69cULYw1yCwNSQyt8QDH6tFWzo6ZRbIVECBzNiRyAY=\" />\r\n</div>\r\n\r\n <div>\r\n \r\n <div class=\"inner-page-title\">\r\n <h2>Welcome to Admintasia 2.3 Live Demonstration</h2>\r\n\t\t\t\t\t <span>You can start building your next user interface with this powerful UI framework !</span>\r\n\t\t\t\t </div>\r\n <div class=\"clear\"></div>\r\n\t\t\t\t <div class=\"content-box\">\r\n\t\t\t\t\t <div class=\"two-column\">\r\n\t\t\t\t\t\t <div class=\"column\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n <div class=\"portlet-header ui-widget-header\">New Release: Admintasia 2.3<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\r\n\t\t\t\t\t\t\t\t\t <p>\r\n\t\t\t\t\t\t\t\t\t\t <a href=\"http://www.admintasia.com\">\r\n <b>Visit the presentation website</b>\r\n </a> | <b><a href=\"http://www.admintasia.com/live-demo\"><b>View Live Demonstration</b></a></b>\r\n\t\t\t\t\t\t\t\t\t\t <br /><br />\r\n Prices start from <b>$50</b> for the <b>Regular License</b> \r\n and <b>$299</b> for the <b>Extended License</b>. Both licenses \r\n come with <b>support access</b> and <b>lifetime updates</b>. \r\n </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t <div class=\"column column-right\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-header ui-widget-header\">Affiliate Program<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\t\t\t\t\t\t\t\t\t\r\n <p> Join our affiliates program and earn 51% of every sale. \r\n <br />\r\n <br /><b><a href=\"http://www.admintasia.com/affiliates/\">Click here for more details about our affiliates program</a></b>.\r\n\t\t\t\t\t\t\t\t\t </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t </div>\r\n\t\t\t\t\t "+ "<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n \r\n\r\n </div>\r\n </form>\r\n <script type=\"text/javascript\">\r\n $(document).ready(function () {\r\n parent.setheight(document.body.clientHeight);\r\n });\r\n function setheight() {\r\n parent.setheight(814);\r\n }\r\n <\/script>\r\n</body>\r\n</html>\r\n";var rx = /<body[^>]*>([\s\S]+?)<\/body>/i///////////var m = rx.exec(s);if (m) m = m[1];alert(m) js函数传入多级菜单项 大家参谋下 作用域内的似有成员 都存放在哪? FF正常,IE报错 怎样用脚本控制另外一个窗口对象的属性? 请问如何修改alert和confirm()弹出窗口的文字字体颜色 求定时刷新网页你码 怎樣讓checkbox實現radio的功能? 请教一下基于bootstrap的switch的问题 如何调用外部js文件 点击盒子变颜色 js字符串正则取值问题 js访问框架的问题
??不是很明白楼主的意思
var bodyHTML;
try
{
var htmlStr = eval('('+str+')');
var bodyHTML = htmlStr.body.innerHTML;
}
catch(e)
{
}
我用EmEditor打开后搜索时在自定义里边吧.匹配换行符选中和.匹配换行符的行数改大后用那个正则匹配可以的
要不版主的那个匹配字符串可以 但在编辑器里边不行
结果是
<body><a>12312312312312</a></body>
可以匹配的到
<body>
<a>12312312312312</a>
</body>
无法匹配
------------------------------------------------
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<a>12312312312312</a>
</body>
</html>这种没问题。稍微复杂一点就匹配不到
如:\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>\r\n\r\n</title>\r\n <script type=\"text/javascript\" src=\"js/jquery-1.6.2.js\"></script>\r\n\t<script type=\"text/javascript\" src=\"js/custom.js\"></script>\r\n\r\n\t<link href=\"css/ui/ui.base.css\" rel=\"stylesheet\" media=\"all\" /><link href=\"css/themes/gray_standard/ui.css\" rel=\"stylesheet\" title=\"style\" media=\"all\" />\r\n\r\n\t<!--[if IE 6]>\r\n\t<link href=\"css/ie6.css\" rel=\"stylesheet\" media=\"all\" />\r\n\t<script src=\"js/pngfix.js\"></script>\r\n\t<script>\r\n\t /* Fix IE6 Transparent PNG */\r\n\t DD_belatedPNG.fix(\'.logo, ul#dashboard-buttons li a, .response-msg, #search-bar input\');\r\n\t</script>\r\n\t<![endif]-->\r\n \r\n</head>\r\n<body onunload=\"setheight()\">\r\n <form method=\"post\" action=\"welcome.aspx?action=get&name=\" id=\"form1\">\r\n<div class=\"aspNetHidden\">\r\n<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/wEPDwUKMTY1NDU2MTA1MmRkj69cULYw1yCwNSQyt8QDH6tFWzo6ZRbIVECBzNiRyAY=\" />\r\n</div>\r\n\r\n <div>\r\n \r\n <div class=\"inner-page-title\">\r\n <h2>Welcome to Admintasia 2.3 Live Demonstration</h2>\r\n\t\t\t\t\t <span>You can start building your next user interface with this powerful UI framework !</span>\r\n\t\t\t\t </div>\r\n <div class=\"clear\"></div>\r\n\t\t\t\t <div class=\"content-box\">\r\n\t\t\t\t\t <div class=\"two-column\">\r\n\t\t\t\t\t\t <div class=\"column\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n <div class=\"portlet-header ui-widget-header\">New Release: Admintasia 2.3<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\r\n\t\t\t\t\t\t\t\t\t <p>\r\n\t\t\t\t\t\t\t\t\t\t <a href=\"http://www.admintasia.com\">\r\n <b>Visit the presentation website</b>\r\n </a> | <b><a href=\"http://www.admintasia.com/live-demo\"><b>View Live Demonstration</b></a></b>\r\n\t\t\t\t\t\t\t\t\t\t <br /><br />\r\n Prices start from <b>$50</b> for the <b>Regular License</b> \r\n and <b>$299</b> for the <b>Extended License</b>. Both licenses \r\n come with <b>support access</b> and <b>lifetime updates</b>. \r\n </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t <div class=\"column column-right\">\r\n\t\t\t\t\t\t\t <div class=\"portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all\">\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-header ui-widget-header\">Affiliate Program<span class=\"ui-icon ui-icon-circle-arrow-s\"></span></div>\r\n\t\t\t\t\t\t\t\t <div class=\"portlet-content\">\t\t\t\t\t\t\t\t\t\r\n <p> Join our affiliates program and earn 51% of every sale. \r\n <br />\r\n <br /><b><a href=\"http://www.admintasia.com/affiliates/\">Click here for more details about our affiliates program</a></b>.\r\n\t\t\t\t\t\t\t\t\t </p>\r\n\t\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t\t </div>\r\n\t\t\t\t\t\t </div>\r\n\t\t\t\t\t </div>\r\n\t\t\t\t\t
..............
..............
<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n \r\n\r\n </div>\r\n </form>\r\n <script type=\"text/javascript\">\r\n $(document).ready(function () {\r\n parent.setheight(document.body.clientHeight);\r\n });\r\n function setheight() {\r\n parent.setheight(814);\r\n }\r\n </script>\r\n</body>\r\n</html>\r\n"复杂点上面就匹配不到了。
/<body.*?>([\s\S]*?)<\/body>/
+ "<p> Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus.</p>\r\n\t\t\t\t\t</div>\r\n\t\t\t\t</div>\r\n \r\n\r\n </div>\r\n </form>\r\n <script type=\"text/javascript\">\r\n $(document).ready(function () {\r\n parent.setheight(document.body.clientHeight);\r\n });\r\n function setheight() {\r\n parent.setheight(814);\r\n }\r\n <\/script>\r\n</body>\r\n</html>\r\n";
var rx = /<body[^>]*>([\s\S]+?)<\/body>/i///////////
var m = rx.exec(s);
if (m) m = m[1];
alert(m)