Strip Consecutive Line Breaks From Contenteditable
Solution 1:
The main problem is that the string in comment.content contains non-printable characters (specifically unicode zero width joiner characters), so the string <div><br></div> is not always a match because of this invisible characters.
To get around this you first need to remove those characters from the string and then remove the extra <div><br></div> occurrences.
One way to do it is using gsub along with \p{Cf} regex and the use gsub again to replace the extra <div><br></div>, like this:
comment.content.gsub(/\p{Cf}/, "").gsub(/(<div><br><\/div>)+/, "<div><br></div>")
#=> "Some text<div>Some more text</div><div><br></div>. Here is another Some text<div><br></div><div>Some text</div>"
To completely remove all <div><br></div> occurrences at the end of the string (example 3), you could add another gsub that removes that substring from the end of the string, for example:
comment.content.gsub(/\p{Cf}/, "")
.gsub(/(<div><br><\/div>)+/, "<div><br></div>")
.gsub(/(<div><br><\/div>)+$/, "")
The added $ in the regexp stand for end of string.
Solution 2:
You can do:
"<div><br><br></div><br><br><br><div><br></div><br>".gsub(/(<br>){2,}/, '<br>')
outputs
=> "<div><br></div><br><div><br></div><br>"
basically, match any 2 or more occurrences - {2,} - of group (), <br>, and substitute for <br>. So pick all where <br> appears repeated (more than once) and substitute for a single <br>.
In your actual case you might need to add tolerance to white space, line breaks, etc.
Post a Comment for "Strip Consecutive Line Breaks From Contenteditable"