I'm having a bit of trouble figuring out the correct way to code my regex expression. Basically I want to insert hard line breaks (<br>) into a string. Let's say I want the maximum line length to be 10 characters. I want to insert a <br> at the nearest space before the 11th character, and right before the 11th character if the current line has no spaces within its 10 characters. If the last line has less than 10 characters we do nothing.
Example: Hello there, my name is Bob -> Hello <br>there, my <br>name is <br>Bob
Example: HelloThereMyName -> HelloThere<br>MyName
My current regex expression is:
x.replace(/[\s\S]{1,10}(?!\S)/g, '$&<br>')