Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
build array based on a text with character delimiter

I'm having difficulty creating a generic function where an input would be a string containing a large or small volume of text and the output is an array and I'm having a lot of trouble with this complexity, but each position of the array must contain 125 characters at most and it cannot have a broken word, eg if there is any final word with which 125 character pass must be placed to the next position of the array.

Example:

Input value: `var text =" Lorem Ipsum is simply a text simulation of the printing and printing industry, and has been in use since the 16th century, when an unknown printer took a tray of typefaces and shuffled them together to make a type model book Lorem Ipsum survived not only five centuries but also the leap into desktop publishing, remaining essentially unchanged It became popular in the 1960s when Letraset launched decals containing passages of Lorem Ipsum, and more recently when is now integrated with desktop editing software like Aldus PageMaker." `

Incorrect output value: `[ "Lorem Ipsum is simply a text simulation of the printing and printing industry, and it has been in use since the century," "XVI, when a printer discovered a type tray and shuffled them together to make a typeface template book."] `

Note that the 0 position of the array has gone from 125 because of the word "century".

Correct output value: `[" Lorem Ipsum is simply a text simulation of the printing and printing industry, and has been in use since the "," 16th century, when an unknown printer took a tray of typefaces and shuffled them to make a book of models of "," types. Lorem Ipsum survived not only five centuries, but also the leap into desktop publishing, remaining ", ...]`

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use the regular expression .{1,125}\b which greedily searches for between 1 and 125 characters followed by a word boundary:

const regex = /.{1,125}\b/g;
var text =" Lorem Ipsum is simply a text simulation of the printing and printing industry, and has been in use since the 16th century, when an unknown printer took a tray of typefaces and shuffled them together to make a type model book Lorem Ipsum survived not only five centuries but also the leap into desktop publishing, remaining essentially unchanged It became popular in the 1960s when Letraset launched decals containing passages of Lorem Ipsum, and more recently when is now integrated with desktop editing software like Aldus PageMaker.";

console.log(text.match(regex));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!