M
M
Mo1she2018-09-03 02:10:42
JavaScript
Mo1she, 2018-09-03 02:10:42

Split string into segments of about 50 characters?

There is a long string. I need to split it into segments about 50 characters long. But so that the words that are on the border of the segments are not broken, but placed in one segment, this is important. Therefore, the segment can be 41, 45, 49 characters long, it doesn’t matter, the main thing is that the words do not break and the number of characters in the segment is about 50.
I tried to do this:

let str = 'some long string';
let result = str.match(/.{1,50}/g);

This code, as you understand, divides the string into clear segments of 50 characters each and breaks individual words into pieces. How can I get what I want?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Vyushin, 2018-09-03
@Mo1she

const str = 'Имеется длинная строка. Мне нужно разделить её на сегменты примерно по 50 символов длинной. Но чтобы слова, находящиеся на границе сегментов не разрывались, а помещались в какой-то один сегмент, это важно. Поэтому сегмент может иметь длинну 41, 45, 49 символов, неважно, главное чтобы не разрывались слова и количество символов в сегменте было примерно 50.';
str.match(/.{1,50}(\s|$)/mig)

Let's take a closer look at the regular expression:
. - finds any character. Any character means really ANY character, in any language (Latin, Cyrillic, etc.), including (attentively) spaces and line breaks.
{1,50} — number of repetitions of "any character". Specify that from 1 to 50 times.
(\s|$) - any whitespace character or the end of a line. That is, we explicitly indicate that there must be a space or end of line at the end of the entry. Thus we find divisions of words. It's worth noting that \s will also match the tab and line break characters. So, if words are separated by line breaks or tabs, then this will also be taken into account.
mig - case-insensitive, global search across the entire string, support for multi-line texts.
The result of this example is an array of all occurrences:
/*======== Amended on 09/03/2017 20:30 ========*/
. - finds any character except newline (\n \r \u2028 or \u2029)
In this regard, a more relevant solution would look like this:
const str = 'Имеется длинная строка. Мне нужно разделить её на сегменты примерно по 50 символов длинной. Но чтобы слова, находящиеся на границе сегментов не разрывались, а помещались в какой-то один сегмент, это важно. Поэтому сегмент может иметь длинну 41, 45, 49 символов, неважно, главное чтобы не разрывались слова и количество символов в сегменте было примерно 50.';
str.match(/(.\n*){1,49}(\s|$)/gi)

X
xmoonlight, 2018-09-03
@xmoonlight

/.{1,50}(?:\s|.{0,50}$)/g

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question