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?
现在我确实设法使用正则表达式获取
This is my code :
这是我的代码:
var title = result.match(/<title[^>]*>([^<]+)<\/title>/);
Now how do i get the actuall title after this/ instead of this?
现在我怎么得到这个/之后的实际标题呢?
5 个解决方案
#1
9
load your response html string into a jQuery object like so and retrieve the text
将响应html字符串加载到jQuery对象中,然后检索文本
$(response).find("title").text();
#2
9
.match()
returns array of matches, use
.match()返回匹配数组,使用
var title = result.match(/<title[^>]*>([^<]+)<\/title>/)[1];
to get value in parentheses
在括号中获得价值
#3
2
CODE:
码:
var title = result.match("<title>(.*?)</title>")[1];
#4
2
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);
JS小提琴演示。
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字符串,并从内容周围删除(假定存在的)封闭的 / 标记。但是,即使这些标签在字符串中它仍然有效:JS Fiddle演示。
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);
JS小提琴演示。
#5
0
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!");
}
#1
9
load your response html string into a jQuery object like so and retrieve the text
将响应html字符串加载到jQuery对象中,然后检索文本
$(response).find("title").text();
#2
9
.match()
returns array of matches, use
.match()返回匹配数组,使用
var title = result.match(/<title[^>]*>([^<]+)<\/title>/)[1];
to get value in parentheses
在括号中获得价值
#3
2
CODE:
码:
var title = result.match("<title>(.*?)</title>")[1];
#4
2
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);
JS小提琴演示。
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字符串,并从内容周围删除(假定存在的)封闭的 / 标记。但是,即使这些标签在字符串中它仍然有效:JS Fiddle演示。
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);
JS小提琴演示。
#5
0
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!");
}