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

121
Views
Por qué no puedo unir toda la cadena

Tengo un problema, estoy usando JS. Digamos que tengo este texto separado por párrafos:

prueba hola

prueba1 hola

prueba2 hola

prueba3 hola

y luego quiero unir todos los párrafos uno al lado del otro así: Test hola Test1 hola Test2 hola Test3 hola, así que hice esto:

 <pre> var x = string.trim(); var xArr = x.match(/^\n|\S+/gm); var xJoin = xArr.join(" "); </pre>

pero continúa como antes con los párrafos, también intento con string.replace(). Después de x.match(/^\n|\S+/gm) me trae una matriz como esta:

 <pre> [, , Test, hello , , Test1, hello , , Test2, hello , , Test3, hello , , ] </pre>

Entonces esto me pareció un poco extraño, ¿podría ser que realmente no está separado por saltos de línea? ¿Alguna idea de cómo poner las cuerdas una al lado de la otra? Gracias.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Prueba esto

 const text = `Test hello Test1 hello Test2 hello Test3 hello`; console.log(text.split('\n').filter(t => !!t).join(" "));

about 4 years ago · Juan Pablo Isaza Report

0

Necesita arreglar algunos problemas menores en su código

  1. Necesita usar reemplazar si desea reemplazar la nueva línea con espacio
  2. Necesita eliminar ^ (este comienzo de cadena) de su patrón

 let str = `Test hello Test1 hello Test2 hello Test3 hello` var x = str.trim(); var xArr = x.replace(/\n|\r|\r\n/gm, ' '); console.log(xArr)

about 4 years ago · Juan Pablo Isaza Report

0

¿Tu párrafo está en un elemento html? Si es así, puedes usar esto:

 const textElement = document.getElementById("text"); console.log(textElement.innerText);
 <div id="text"> Test hello Test1 hello Test2 hello Test3 hello </div>

O colóquelo en un elemento html creado a partir de su javascript antes de usar el método anterior.

De lo contrario, ¿puedes probar esta expresión regular? text.replace(/(\r\n|\n|\r)/gm, "");

about 4 years ago · Juan Pablo Isaza 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!