Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda