6

Can someone please help me in finding a regex that checks in a string if it contains one of the following html break tags?

<br>, <br/>, <br >, <br />
Bart
  • 19,692
  • 7
  • 68
  • 77
lollos90
  • 139
  • 1
  • 3
  • 9

2 Answers2

18
  1. Read a tutorial.
  2. If you do more than this on your HTML, don't use regex!
  3. <br\s*/?> and you should make that case-insensitive (depends on the language or tool you are using). If you want to be that strict to really allow only the four versions you posted (and not multiple spaces), cadrian's version is what you are looking for: <br ?/?>
Community
  • 1
  • 1
Martin Ender
  • 43,427
  • 11
  • 90
  • 130
  • 2
    Good answer, just a note/fix: You need to escape the slash, so it comes down to `
    `.
    – Dennis98 Sep 20 '16 at 12:48
  • 1
    @Dennis98 That depends highly on which regex flavour you're using (it only applies to flavours like ECMAScript, Ruby, Perl and PCRE which use `/` as delimiters; even in those it's often possible to use non-`/` delimiters to avoid escaping slashes). – Martin Ender Sep 20 '16 at 12:51
  • Oh, really haven't thought about this. All languages I worked with regexes as of now, which is PHP, JS and Perl (so your already mentioned PCRE, ECMA and Perl), use slashes as delimiters.. Good to know! :) I can't edit my comment anymore, though.. ^^ – Dennis98 Sep 20 '16 at 13:52
4

try this out: "<br ?/?>"

cadrian
  • 7,332
  • 2
  • 33
  • 42