Javascript 如何获取html字符串中的标题标记?
嘿,我正在使用ajax将一个html页面加载到一个字符串中,现在我想找到页面的标题并使用它 现在我确实使用正则表达式获得了Javascript 如何获取html字符串中的标题标记?,javascript,jquery,regex,Javascript,Jquery,Regex,嘿,我正在使用ajax将一个html页面加载到一个字符串中,现在我想找到页面的标题并使用它 现在我确实使用正则表达式获得了,但是它会返回标签和标题本身,我希望从字符串中提取出来,或者在正则表达式中有什么方法可以做到这一点 这是我的代码: var title = result.match(/<title[^>]*>([^<]+)<\/title>/); var title=result.match(/]*>([^.match()返回匹配数组,使用 var ti
,但是它会返回标签和标题本身,我希望从字符串中提取出来,或者在正则表达式中有什么方法可以做到这一点
这是我的代码:
var title = result.match(/<title[^>]*>([^<]+)<\/title>/);
var title=result.match(/]*>([^.match()
返回匹配数组,使用
var title = result.match(/<title[^>]*>([^<]+)<\/title>/)[1];
var title=result.match(/]*>([^代码:
var title = result.match("<title>(.*?)</title>")[1];
var title=result.match((*?)[1];
将响应html字符串加载到类似so的jQuery对象中,然后检索文本
$(response).find("title").text();
将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!");
}
var regex=/(.*)/gi;
var input=“你好,世界……”;
if(正则表达式测试(输入)){
var matches=input.match(regex);
for(匹配中的变量匹配){
警报(匹配[匹配]);
}
}否则{
警报(“未找到匹配项!”);
}
一种相对简单的纯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);
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);
var htmlString='某个标题某个段落中的某个文本!,
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);
显然,我不得不猜测您的HTML字符串,并从内容周围删除了(假定存在)包含
/
标记。但是,即使这些标记在字符串中,它仍然有效:
还有一种更实用的方法:
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);
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);
函数textFromHTMLString(html,目标){
如果(!html | |!target){
返回false;
}
否则{
var fragment=document.createDocumentFragment(),
container=document.createElement('div');
container.innerHTML=html;
片段.appendChild(容器);
var targets=fragment.firstChild.getElementsByTagName(目标),
结果=[];
对于(var i=0,len=targets.length;i试试这个,我认为这会有帮助。它在我的情况下非常有效。:)
使用jquery来选择标题标记…不要使用正则表达式。它在文档中没有的字符串中。我知道这一点,但我看到了解析HTML字符串并对其进行操作的解决方案。编辑:发现如果您向我显示一些指向这些解决方案的链接,我会很高兴,因为这可以节省我很多时间。@nhahtdh如果您作为答案提交,我会投票,然后提交者可能应该接受它。这是最干净的方法。谢谢你,伊万。这确实有效。但是有没有更好的方法来获取标题标记?如果你使用html作为字符串,你可以使用/(.*)/i
regexp。如果你使用jquery,你可以创建文档片段并从中选择值$(你的HTMLSTRING)。查找('title')。text()
该文档是对ajax请求的响应。因此可能无法通过document.title访问该文档。由于某种原因(jQuery 1.9.1),这对我来说不起作用,我不得不将响应放在div中,并将其加载到jQuery对象中:var div=document.createElement('div');div.innerHTML=response;$(div.find('title')。text()
惊人的答案,没有正则表达式!!!我一直在用子字符串和长度猛击我的头,试图从html字符串中提取第一、第二、第三个img
标记。现在这太容易了!!!