javascript 如何获取一串html中的标题标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13452865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get title tag in a string of html?
提问by eric.itzhak
Hey i'm loading an html page using ajax into a string, now i want to find the title of the page and use it.
嘿,我正在使用 ajax 将 html 页面加载到字符串中,现在我想找到页面的标题并使用它。
Now i did manage to get the <title>
using regex but that returns the tag along with the title itself and i wish to extract that from the string or could there be a way to do that in the regex?
现在我确实设法<title>
使用了正则表达式,但它返回了标签和标题本身,我希望从字符串中提取它,或者有没有办法在正则表达式中做到这一点?
This is my code :
这是我的代码:
var title = result.match(/<title[^>]*>([^<]+)<\/title>/);
Now how do i get the actuall title after this/ instead of this?
现在我如何在这个/而不是这个之后获得实际的标题?
回答by Ivan Solntsev
.match()
returns array of matches, use
.match()
返回匹配数组,使用
var title = result.match(/<title[^>]*>([^<]+)<\/title>/)[1];
to get value in parentheses
获取括号中的值
回答by Bruno
load your response html string into a jQuery object like so and retrieve the text
像这样将您的响应 html 字符串加载到 jQuery 对象中并检索文本
$(response).find("title").text();
回答by David says reinstate Monica
A relatively simple plain-JavaScript, and non-regex, approach:
一种相对简单的纯 JavaScript 和非正则表达式方法:
var htmlString = '<head><title>Some title</title></head><body><p>Some text, in a paragraph!</p></body>',
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);?
I've, obviously, had to guess at your HTML string and removed the (presumed-present) enclosing <html>
/</html>
tags from around the content. However, even if those tags are in the string it still works: JS Fiddle demo.
显然,我不得不猜测您的 HTML 字符串并从内容周围删除(假定存在的)封闭<html>
/</html>
标签。但是,即使这些标签在字符串中,它仍然有效:JS Fiddle demo。
And a slightly more functional approach:
还有一个稍微更实用的方法:
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<len; i++) {
result.push(targets[i].textContent || targets[i].innerText);
}
return result;
}
}
var htmlString = '<html><head><title>Some title</title></head><body><p>Some text, in a paragraph!</p></body></html>';
var titleText = textFromHTMLString(htmlString, 'title');
console.log(titleText);?
回答by pala?н
CODE:
代码:
var title = result.match("<title>(.*?)</title>")[1];
回答by devsathish
Make the reg exp to case insensitive. Here is the complete code:
使 reg exp 不区分大小写。这是完整的代码:
var regex = /<title>(.*?)<\/title>/gi;
var input = "<html><head><title>Hello World</title></head>...</html>";
if(regex.test(input)) {
var matches = input.match(regex);
for(var match in matches) {
alert(matches[match]);
}
} else {
alert("No matches found!");
}
回答by Iron shield
try this I think this will help. It perfectly works in my case. :)
试试这个我认为这会有所帮助。它完全适用于我的情况。:)
var FindTag=(data='',tag='')=>{
var div=document.createElement('div');
div.innerHTML=data;
data=$(div).find(tag)[0].outerHTML;
return data;
}
var data=FindTag(data,"title");