Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用jQuery解析这个HTML?_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 如何使用jQuery解析这个HTML?

Javascript 如何使用jQuery解析这个HTML?,javascript,jquery,regex,Javascript,Jquery,Regex,在过去的两个小时里想弄清楚这件事简直是疯了。我将此html作为字符串从AJAX请求返回: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head>

在过去的两个小时里想弄清楚这件事简直是疯了。我将此html作为字符串从AJAX请求返回:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Preview</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="author" content="Connected Ventures LLC. Copyright 1999-2010." />
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/jquery.ui.js"></script>
    <script type="text/javascript" src="js/article.js"></script>
    <link href="/css/global.css" rel="stylesheet" type="text/css" />
    <link href="/css/article.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
    html, body { background: #fff; color: #000; }
    </style>
</head>
<body class="the_article">
        <p>s</p></body>
</html>
没用。即使添加了:

dataType: 'html'
作为ajax请求参数。然后我尝试使用正则表达式解析它:

ajax_response.match(/<body class="the_article">.*?<\/body>/); 
ajax\u response.match(/.*?/);

它只是通知空。你知道如何获取正文内容吗?

你的正则表达式失败了,因为字符串是多行的,
通配符匹配除空白字符以外的所有字符,所以在开头的
body
标记和正文内容之后的换行符会破坏模式

使用
[\s\s]
而不是
(字面上,允许使用非空格和空格字符)

/[\s\s]*/
[编辑]-响应注释,要捕获不包含其标记的正文内容,请将内容捕获为子组:

var body = response.match(/<body class="the_article">([\s\S]*?)(?=<\/body>)/);
console.log(body[1]); //body content, not including tag
var body=response.match(/([\s\s]*?)(?=)/);
控制台日志(正文[1])//正文内容,不包括标签

请注意,我们还将结束正文标记指定为前瞻,因为我们根本不需要匹配它,只需锚定到它。(JS不支持look behinds,缺少模拟,因此我们别无选择,只能捕获开头的body标记)。

您可以让dom为您完成这项工作。
将代码插入带有document.write的iframe中,然后访问frame.document.body.innerHTML属性。

@Zee-Tee尝试了它,只是警告null。您的代码应该可以工作,问题一定在其他地方……您尝试过记录
ajax\u响应吗?你得到了什么?改用javascript,
substr
@elclanrs将响应粘贴到问题I did console.log中,这样响应是正确的,它包含正确的html。@JakeRow123:回调中的响应是什么,是字符串还是DOM?有一件事,我如何只获取body标记之间的内容。这也会返回主体标记本身。使用捕获组:
/([\s\s]*?)/
这个答案的意思是正确的,但您不想使用
文档。编写
(邪恶,对于页面加载后执行的任何JS都不可行),文档片段是比iframe更好的选择。是的,他尝试过。但是jQuery不为您处理iframe吗?@Utkanos:我不确定documentFragment是否会对doctype声明感到高兴。
/<body class="the_article">[\s\S]*?<\/body>/
var body = response.match(/<body class="the_article">([\s\S]*?)(?=<\/body>)/);
console.log(body[1]); //body content, not including tag