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

133
Visualizações
Javascript: Add Dynamic Text to Markdown

I have some markdown content. For example:

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`

What I need to do is add a string like inbetween this markdown, after the 4th line break. So, it should appear after Title 1, and the new markdown should look as follows:

`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`

The problem is that I don't know how to look for line breaks in javascript.

How can I do this in javascript? The result should still be valid markdown.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could split the string by line breaks.

Then loop through the elements in the array, and then add the text after the nth element.

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

0

This snippet allows you to insert some insert string immediately after the line (only non-blank lines are considered) specified by insertAfter index. Without looping.

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 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