简体   繁体   English

正则表达式以匹配包含以下内容的字符串 <br/> 但不是其他HTML标签?

[英]Regular expression to match strings that contain <br/> but not other HTML tags?

I want to find and match strings that contain no HTML tags but <br/> and all other normal characters ([^<>]+). 我想查找和匹配不包含HTML标记但包含<br/>和所有其他普通字符([^ <>] +)的字符串。

So basically, this match dismisses any string that contains '<' or '>' but not '<br/>'. 所以基本上,这个匹配解除了包含'<'或'>'但不包含'<br/>'的任何字符串。

This is what I can come up with: 这就是我能想到的:

preg_match('@[(?:<br/>).]+@sU', $str, $match);

Obviously it doesn't work cause' I don't know what to put at the dot. 显然它不起作用因为'我不知道该把什么放在点上。 Any ideas? 有任何想法吗?

I would just do it backwards -- see if the string contains any <...> tag other than <br/> and dismiss it if it does. 我只是向后做 - 看看字符串是否包含除了<br/>之外的任何<...>标记,如果有,则将其解除。 So: 所以:

preg_match(/<(?!br)/i, $str, $match);
if (!$match) we_are_good();

Why not... 为什么不...

@(?:[^<]|<br */>)*@

That is, any number of (a complete <br /> tag or any non-tag-opening character). 也就是说,任意数量的(一个完整的<br />标签或任何非标签打开的字符)。

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

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