Extract title tags from normal text

Posted by pravin on Stack Overflow See other posts from Stack Overflow or by pravin
Published on 2010-06-07T08:39:24Z Indexed on 2010/06/07 8:42 UTC
Read the original article Hit count: 252

Filed under:
|

I am working on one task, to extract title tag from given normal text ( it's not a HTML DOM ). I have below cases where need to extract title tag(s) :

Case 1 :

<html>
<head>
           <title>Title of the document</title>
</head>
<body>
The content of the document......
</body>
</html>

Expected : Title of the document

Case 2 :

<html>
<head>
           <title>Title of the document</title>
           <title>Continuing title</title>
</head>
<body>
The content of the document......
</body>
</html>

Expected : Title of the document Continuing title

Case 3 (Nested title tags)

<html>
<head>
           <title>Title of the document
           <title>Continuing title</title></title>
</head>
<body>
The content of the document......
</body>
</html>

Expected : Title of the document Continuing title

I wanted to extract title tags using regular expression in javascript. Reg-ex should work for above case.

Is anyone knows about this..please let me know... Thanks in Advance

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex