MV Text Message Parse with Break Lines

Hello everyone!
I'd like to put automated Break Lines on a specific message text.
I don't know if the message will be just 1 word of 3 letters or a long word of 100 letters.
What's the best way to display it on a message? Is there a way to script code it --> break line every X letters?
one of yanfly's messaging plugins adds a wordwrap feature that does this automatically.
Yes I know, but it conflicted with other plugins.
I'd like to just call a small script code with the window with text and an auto-breaklines, it's for a single message box.
this is pretty easy to do if you know any js. search "word wrap in javascript" ... take that function and send your message through it, then use $gameMessage.add(wrappedLine)
automatic breaklines don't work that way.
you can either use a manual break, or you'll need a wordwrap type plugin for automatic function.
Tried a little but this won't work:

Code:
let surname = str.replace(/([^\n]{1,32})\s/g, '[$1]\n');
$gameMessage.add(surname);
Tried a little but this won't work:
of course not, because you're missing the context where that function would work.
and the way you used that function basically shows that you don't have enough programming knowledge to add the correct context yourself.

please look for a wordwrap plugin to provide the correct context, and if you have compatibility issues with that then ask for help in solving that compatibility problem.
As a general tip, only telling us that something "won't work" does very little to help us help you. It could be anything, starting from your code not even being executed (you didn't tell us how you call that piece of code) to any number to possible errors in your code. Like is str even defined? Or do you get some kind of "can't access property replace of undefined" error on the console? Maybe there is an error with your RegEx? Maybe everything in your first line is fine, but you simply try to add your surname at the wrong time or with the wrong function?
If you want to get help, you need to provide us with more detail on what exactly doesn't work.
automatic breaklines don't work that way.
you can either use a manual break, or you'll need a wordwrap type plugin for automatic function.
not sure if you're replying to me or OP, but i've done it this way in plugins i've written and it works fine.

i'm a novice so this might be a bad solution, but i got it to work for my purposes.

here's the function i used (I'm replacing my variable with surname to help OP) :

the stringDivider function takes 3 arguments - the variable you want to change, in this case surname, followed by the number of characters before line break, and then the line break symbol, in this case '\n'

JavaScript:
surname = stringDivider(surname, 70, '\n')

function stringDivider(surname, width, spaceReplacer) {
    if (surname.length>width) {
        var p=width
        for (;p>0 && surname[p]!=' ';p--) {
        }
        if (p>0) {
            var left = surname.substring(0, p);
            var right = surname.substring(p+1);
            return left + spaceReplacer + stringDivider(right, width, spaceReplacer);
        }
    }
    return surname;
}
not sure if you're replying to me or OP
to the OP, your post wasn't even visible to me before posting.
and yes, it can be done in plugins which is why I told the OP to look for one such plugin.
it just can't work the way the OP assumed it to work with a simple script command.
of course not, because you're missing the context where that function would work.
and the way you used that function basically shows that you don't have enough programming knowledge to add the correct context yourself.

please look for a wordwrap plugin to provide the correct context, and if you have compatibility issues with that then ask for help in solving that compatibility problem.
This forum is intended to help, not to get offended..

If I knew the solution... that's why I asked, because I don't have.

Anyway, a script command should be enough to just put break lines on a single string ranging from 10-20. A full plugin isn't viable because I should check too many incompatibilities for just that worthless message wrap. I'd otherwise change the whole interaction with that text, if script call can't fix it alone. But it should...
This forum is intended to help, not to get offended..

If I knew the solution... that's why I asked, because I don't have.

Anyway, a script command should be enough to just put break lines on a single string ranging from 10-20. A full plugin isn't viable because I should check too many incompatibilities for just that worthless message wrap. I'd otherwise change the whole interaction with that text, if script call can't fix it alone. But it should...
Sometimes people get frustrated with each other and it can derail the thread. Let's stay focused on solving the problem : )

I've provided a wordwrap function and shown how to use it above. You don't have to put it in a plugin if you don't want to. You can probably put it in a script command in a common event or you can put it in a script command in every event you want to run, it's up to you how to implement it.
Sometimes people get frustrated with each other and it can derail the thread. Let's stay focused on solving the problem : )

I've provided a wordwrap function and shown how to use it above. You don't have to put it in a plugin if you don't want to. You can probably put it in a script command in a common event or you can put it in a script command in every event you want to run, it's up to you how to implement it.
I just tested that small snippet @Starbird_Resources provided and it works well in both MV and MZ. I tested as both a one-time map event and a common event. The only thing I had to change was the width variable if I used a face. MZ worked well with 48, MV required 42. That's using the default font. If you change your font /size, you'll probably need to adjust accordingly.

This is such a simple and useful script. Loving it. 😂
That was the help I needed ! Thank you very much!

Original thread: https://forums.rpgmakerweb.com/threads/text-message-parse-with-break-lines.143140/