When checking for whitespace and <br />
, you should use an alternation with zero or more repeats, anchored at the start and end:
if(/^(?:\s|<br \/>)*$/.test(content)){ }
This breakdown is as follows:
^
- indicates the start of input
(?:...)
- a non-capturing group
\s
- represents any single whitespace character
|
- signifies an alternation between two options
<br \/>
- matches the literal string <br />
(escaping the /
)
*
- allows for zero or multiple occurrences of the preceding group
$
- marks the end of input
If there is a possibility that the space or solidus within the tag may be missing, or the space may be repeated, you can modify the regex to:
if(/^(?:\s|<br *\/?>)*$/.test(content)){ }
Adding *
after the space allows for zero or multiple spaces; adding ?
after the solidus (/
) makes it optional.
Please note: The above patterns do not account for attributes on the br
tag, such as <br class="foo" />
, <br data-foo="bar"/>
, etc. Additionally, they match empty strings.
To include attributes while still matching empty strings:
if(/^(?:\s|<br[^>]*>)*$/.test(content)){ }
// Change ----^^^^
To consider attributes and require at least one whitespace or br
tag (eliminating empty strings):
if(/^(?:\s|<br[^>]*>)+$/.test(content)){ }
// Change ------- ---^