Finds a position to break a string that is not part of a
You want to check if the letter that you have matched is part of a &lt;br&gt;.
But you're doing a strange check. That has bugs.

Lets say we test:


a&lt;bc&gt;d&lt;br&gt;



we say that maxLength is 1.
You'll not break the &lt;bc&gt;.
For a dry test of this:

text.includes('&lt;br&gt;') # true
text.charAt(maxLength).toLowerCase() # '&lt;'
switch ('&lt;')
case '&lt;':
text.charAt(maxLength + 3).toLowerCase() === '&gt;' # true
maxLength -= 1 # maxLength = 0


Instead you can use, indexOf, slice and length to check if the text is not allowed.
This allows for you to easily change what you want to remove, and to expand on it.
Whilst being much smaller than what you have.


Get the, lowercase, charAt.
Find the indexof it in the remove string.
Set the start as maxLength minus the index.
Check if the index is in the remove string, and check if the slice of text from start to start plus the remove strings length is the remove string.
If it is change the maxLength to start - 1.


Or in JavaScript:

var text = 'a&lt;br&gt;d&lt;br&gt;';
var remove = '&lt;br&gt;';
var maxLength = 4;

var char = text.charAt(maxLength).toLowerCase();
var index = remove.indexOf(char);
var start = maxLength - index;
if (index != -1 &amp;&amp; text.slice(start, start + remove.length).toLowerCase() == remove) {
    maxLength = start - 1;
}