简体   繁体   中英

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM