-2
<title>

    Ask a Question - Stack Overflow Ask a Question - Stack Overflow

</title>

when i was try to catch title empty. my pattern is

<title[^>]*>([^<]+)</title>

how can i remove empty lines or catch only title text?.

thank you

B. Mert
  • 19
  • 5
  • Are you trying to do this on the server, or in JavaScript? It you're trying to do this is JavaScript, do you have to use a regular expression? – KevBot Jul 09 '17 at 22:46
  • You won't be able to reliably parse HTML using a regular expression. You'll get better results with an HTML parser. See also: https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Mate Solymosi Jul 09 '17 at 22:47

1 Answers1

0

www.regexpal.com is a great utility for answering these types of questions. The m flag on a RegEx object will catch multiple lines nicely. This should match everything in between the title tags in the first matching group, even if it's multi-line, I always use the i flag as well, just to handle the unknowns if you're working with 3rd party data:

/<title>([^<]+)<\/title>/im

If you'd like the whitepsace removed (i.e. trim), you could do something like:

/<title>(?:\s+)?([^<]+)(?:\s+)?<\/title>/im

In either case, your first match group should have what you'd like:

myText.match(/<title>(?:\s+)?([^<]+)(?:\s+)?<\/title>/im)[1]
clint
  • 301
  • 4
  • 9