简体   繁体   English

正则表达式中不匹配的html标签

[英]html tags not matching in regex

I have a string that contains an html document. 我有一个包含html文档的字符串。 I need to know if this string contains the substring <title>Anmelden - Text</title> . 我需要知道此字符串是否包含子字符串<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. 但是idx始终为-1。 If I remove the <title> and </title> the expression works. 如果删除<title></title>则表达式有效。

I have used http://regexpal.com/ to verify my regex. 我已经使用http://regexpal.com/来验证我的正则表达式。 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. 使用[\\S\\s]*代替\\n*.*.*\\n*因为在换行符后可能会有空格。 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. 请注意, \\n仅匹配换行符,而\\s匹配包括换行符\\n ,回车符\\r和制表符\\t在内的所有空格字符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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