Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda