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

296
Vistas
Use of regex in replace() to join split words in a text

I have this sample sentences in a plain text file (utf-8):

today is an interest-
ing day

the "-" on the first line is followed by only \n (I have already stripped all \r from the file, to deal uniformy with different sources) I would like to wrap the 2 lines in 1 line, because of the "-", meaning that the preceding word has been truncated and is continuing in the next line. To join this kind of lines, what I have tried is something along the lines:

text.replace(/[\n-]/g, "") 

but does not seem to be working. What is the right way to achieve this ?

I would like to be able to deal with both these possible endings (or similar situations you might anticipate):

interest-\n
interest- \n    (possible blanks inserted before \n)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use

text.replace(/\b-\s*\n\b/g, "")
text.replace(/\b-[^\S\r\n]*\n\b/g, "")

See the regex demo. Details:

  • \b - a word boundary
  • - - a hyphen
  • \s* - zero or more whitespaces / [^\S\r\n]* - zero or more horizontal whitespaces (supporting CRLF, CR and LF endings)
  • \n - a newline char
  • \b - a word boundary.

See the JavaScript demo:

console.log( "today is an interest- \ning day".replace(/\b-\s*\n\b/g, "") );
console.log( "today is an interest-\ning day".replace(/\b-\s*\n\b/g, "") );

A Unicode-aware pattern that checks for just letters on both ends can look like text.replace(/(?<=\p{L}\p{M}*)-[^\S\r\n]*\n(?=\p{L})/gu, ""), where (?<=\p{L}\p{M}*) checks for a letter + optional diacritics before - and (?=\p{L}) checks for a letter after a newline. See the regex demo.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are three things wrong in your regex for this use:

  1. You have the new line before the -

  2. The [] means a list of characters to match at least one of them

  3. You need to add \s to match whitespace

So try this:

text.replace(/-\s*\n/g, "") 
about 4 years ago · Juan Pablo Isaza 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