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

189
Views
split string based on character limit and a period

I want to split a string based on 30 character limit and the catch here is it should get broken on a period(.) or a comma(,) or a space( ). i.e., the string length of 30 or a delimiter whichever comes first. I tried with the match using regex, this splits well, but not with a delimiter.

For example, Here is my sample code.

let x='Lorem ipsum dolor sit amet. Excepteur cuptat non proident, consectetur adipiscing elite detailo';

let splitString = x.match(/.{1,30}/g);

console.log(splitString);

Here the output I get is

[
  "Lorem ipsum dolor sit amet. Ex",
  "cepteur cuptat non proident, c",
  "onsectetur adipiscing elite de",
  "tailo"
]

But I want this to happen using a comma or period or a space as a delimiter. Expected output is

[
  "Lorem ipsum dolor sit amet.",
  "Excepteur cuptat non proident,",
  "consectetur adipiscing elite",
  "detailo"
]
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You could match against this regex:

[^\s].{1,29}((?=\s|$)|(?<=[.,]))

which looks for a non-space character ([^\s]), and then up to 29 other characters (.{1,29}) which either:

  1. are followed by a space or end-of-line (?=\s|$); or
  2. end with a comma or full-stop (?<=[.,])

let x='Lorem ipsum dolor sit amet. Excepteur cuptat non proident, consectetur adipiscing elite detailo';

let splitString = x.match(/[^\s].{1,29}((?=\s|$)|(?<=[.,]))/g);

console.log(splitString);

about 4 years ago · Santiago Trujillo Report

0

let x='Lorem ipsum dolor sit amet. Excepteur cuptat non proident, consectetur adipiscing elite detailo';

let splitString = x.match(/.{1,30}[\.\s,]/g).map(item => item.trim());
const remaining = x.replace(splitString.join(' '), '');
splitString = [...splitString, remaining.trim()]

console.log(splitString);

Edit:

I removed the trailing spaces in the substrings. Don't know if it's necessary but there is the exact wanted result now.

about 4 years ago · Santiago Trujillo 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!