admin管理员组

文章数量:1323342

I simply need to extract some info between two tags, in this case, <title>

For example:

...
<title>I Need This!</title>
...

And I simply need to be able to get the information between the tags. I was thinking using split(), however, I haven't been able to figure out how to cut all data before and after, and just catch the stuff in the title tags. As you can tell, I'm a beginner with text formatting. Thanks!

EDIT: An example of the type of file I'm looking through is here: ://youtu.be/_OBlgSz8sSMg?v=2

I'm simply trying to take what's in the title tags to get the title of the video.

I simply need to extract some info between two tags, in this case, <title>

For example:

...
<title>I Need This!</title>
...

And I simply need to be able to get the information between the tags. I was thinking using split(), however, I haven't been able to figure out how to cut all data before and after, and just catch the stuff in the title tags. As you can tell, I'm a beginner with text formatting. Thanks!

EDIT: An example of the type of file I'm looking through is here: https://gdata.youtube./feeds/api/videos/http://youtu.be/_OBlgSz8sSMg?v=2

I'm simply trying to take what's in the title tags to get the title of the video.

Share Improve this question edited Jul 23, 2012 at 17:32 Jtaylorapps asked Jul 23, 2012 at 17:22 JtaylorappsJtaylorapps 5,7809 gold badges42 silver badges57 bronze badges 3
  • Are you parsing HTML text or do you want to pull the title from the current page? If the former, I would remend a simple regular expression to strip away the HTML tags. – Cᴏʀʏ Commented Jul 23, 2012 at 17:24
  • Use an XML parser. Or do you have something specific? – Bergi Commented Jul 23, 2012 at 17:24
  • It's specific. I get a value returned, which is a huge text (the XML code). Then I parse the text, searching for the title tags, and then I collect the value – Jtaylorapps Commented Jul 23, 2012 at 17:27
Add a ment  | 

2 Answers 2

Reset to default 7
var text = '<title>I Need This!</title>',
    match = text.match(/<title>([^<]*)<\/title>/),
    youGotThis = match[1];

Here's the fiddle: http://jsfiddle/RPbSE/

DOM works on XML just as well as on HTML, so a simple

var titleNodeList = yourXMLDocument.getElementsByTagname('title');
var firstTitle = titleNodeList[0];
var titleTextNode = firstTitle.firstChild;
alert(titleTextNode);

should do.

本文标签: javascriptJSRead Data Between XML TagsStack Overflow