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

130
Views
Split a string in two, keeping words intact and returning them

As the title says, looking to see how I can split a string into two parts, while keeping full words intact, and having access to both parts with JavaScript. I am using this string in a pug file.

For instance, I have a very long string

let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.'

I want the first 70 characters, to use as a headline, then the rest to use for the body of the element.

I have used this regex that I found on an older post that gives me the first 70+ characters, with full words...

str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1")

which does what I want it to do! But now I need to figure out if there is a way to get the rest of that string?

str1 = `hello i am a very long string that needs to be split into two different`
str2 = `parts! thank you so much for helping.`

Again, using this in a pug file, so if there is a simple answer, I would love that. Otherwise I suppose I could just write a function and import the script to the pug view.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

What would you want to do when the split happens on a whitespace character. Say; the space right after 'different'. It would mean that the part prior to the space is 69 characters, but 70 if you'd include the space. Assuming you want to capture the next word for good measure, try to utilize JS's zero-width lookbehind:

(?<=^.{70}\S*)\s+

See an online demo


  • (?<= - Open positive lookbehind;
    • ^.{70} - 70 Characters right after start-line anchor;
    • \S* - 0+ Non-whitespace chars;
  • \s+ - Capture 1+ whitespace characters to split on.

const str = 'hello i am a very long string that needs to be split into two different parts! thank you so much for helping';

const result = str.split(/(?<=^.{70}(?:\S)*)\s+/);

console.log(result);

about 4 years ago · Santiago Trujillo Report

0

After extracting the first part of the string using your method you can use the length of str1 to determine where in str the second part begins. From there you can use substring to extract it.

let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.`;
let str1 = str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1");
let str2 = str.substring(str1.length).trimStart();

Using trimStart you can to remove any whitespace that would appear at the start of str2.

about 4 years ago · Santiago Trujillo Report

0

An alternative solution to the previous one, but using only regex would be the following:

let listStrings = str.split(/^([\s\S]{70}[^\s]*)[\s\S](.*)/)).filter(Boolean)

I added to your regex the last part (.*) to obtain the rest of the string as another group. Then, using the javascript string split function you can apply the regex and get a list of strings, being those the groups obtained by the regex.

The .filter(Boolean) is used to remove the empty strings obtained in the response:

0:""
1:"hello i am a very long string that needs to be split into two different"
2:"parts! thank you so much for helping."
3:""
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!