Martin
Martin

Reputation: 862

html tags not matching in regex

I have a string that contains an html document. I need to know if this string contains the substring <title>Anmelden - Text</title>. Unfortunately there are some new lines in the string, so that the string looks like this:

...
<title>
        Anmelden - Text
</title></head>
...

I have tried the following code:

var idx = html.search( /<title>\n*.*Anmelden.*\n*<\/title>/ );

But idx is always -1. If I remove the <title>and </title>the expression works.

I have used http://regexpal.com/ to verify my regex. There it works on my input.

What am I doing wrong?

Upvotes: 0

Views: 67

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174836

Use [\S\s]* instead of \n*.* and .*\n* because there may be a possibility of spaces after the newline character. Note that \n matches only the newline character but \s matches all the space characters including newline \n , carriage return \r, tab characters \t also.

<title>[\S\s]*?Anmelden[\S\s]*?<\/title>

Upvotes: 3

Related Questions