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

132
Views
Javascript: agregar texto dinámico a Markdown

Tengo algo de contenido rebajado. Por ejemplo:

 let text = `This is line one, This is para 2 ![Tux, the Linux mascot](/assets/images/tux.png) **This is title 1** This is para 3 **This is title 2** This is para 4`

Lo que debo hacer es agregar una cadena como entre esta reducción, después del cuarto salto de línea. Por lo tanto, debería aparecer después del Título 1 y la nueva rebaja debería tener el siguiente aspecto:

 `This is line one, This is para 2 ![Tux, the Linux mascot](/assets/images/tux.png) **This is title 1** <Ad /> This is para 3 **This is title 2** This is para 4`

El problema es que no sé cómo buscar saltos de línea en javascript.

¿Cómo puedo hacer esto en javascript? El resultado debería seguir siendo una rebaja válida.

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

0

Podría dividir la cadena por saltos de línea.

Luego recorra los elementos de la matriz y luego agregue el texto después del elemento n.

 let text = `This is line one, This is para 2 ![Tux, the Linux mascot](/assets/images/tux.png) **This is title 1** This is para 3 **This is title 2** This is para 4`; let split = text.split(/\n/g).filter(function(el) { return el; }) let newString = ''; split.forEach(function(value, key) { if (key == 3) { newString += value + '\n\n' + '<Ad />\n\n'; } else { newString += value + '\n\n'; } }); console.log(newString)

about 4 years ago · Juan Pablo Isaza Report

0

Este fragmento le permite insert una cadena de inserción inmediatamente después de la línea (solo se consideran las líneas que no están en blanco) especificada por el índice insertAfter . Sin bucle.

 const input = `This is line one, This is para 2 ![Tux, the Linux mascot](/assets/images/tux.png) **This is title 1** This is para 3 **This is title 2** This is para 4`; const insert = '\n\n<Ad />'; const insertAfter = 3; const match = input.matchAll(/\S\n/g); const position = Array.from(match)[insertAfter].index + 1; const output = [input.slice(0, position), insert, input.slice(position)].join(''); console.log(output);

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!