Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using regex to match <br> or <br/> in a string [closed]

Tags:

regex

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 />
like image 351
lollos90 Avatar asked Nov 08 '12 09:11

lollos90


People also ask

How do you match line breaks in regex?

If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”. Whether or not you will have line breaks in your expression depends on what you are trying to match. Line breaks can be useful “anchors” that define where some pattern occurs in relation to the beginning or end of a line.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you check if a regex matches a string?

Use the test() method to check if a regular expression matches an entire string, e.g. /^hello$/. test(str) . The caret ^ and dollar sign $ match the beginning and end of the string. The test method returns true if the regex matches the entire string, and false otherwise.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


2 Answers

  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 ?/?>
like image 128
Martin Ender Avatar answered Sep 30 '22 19:09

Martin Ender


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

like image 26
cadrian Avatar answered Sep 30 '22 17:09

cadrian