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

143
Vistas
JavaScript replace with an exception string

I have the following html tag (which is custom):

<meltdown-code data-lang="HTML">
    <span><br>
        <p>Hi</p><br>
    </span><br>
</meltdown-code>

Simple enough. Now, I'm trying to make the HTML tags show as plain text, instead of being rendered as a separate element. To do that, I am replacing all the < with &lt;, and all the > with &gt;

tagHTML.replace(/</g, '&lt;')

The problem: This also replaces the <br> tags, which doesn't create a new line. Here is the solution, in a perfect world:

tagHTML.replace(/</g, '&lt;', {exceptions: '<br>'})

Obviously, that's not how it works. Any help is appreciated.

Thanks!

almost 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Negative lookahead for <br> before matching <.

const text = `<meltdown-code data-lang="HTML">
    <span><br>
        <p>Hi</p><br>
    </span><br>
</meltdown-code>`;

const output = text.replace(/(?!<br>)<([^>]+)>/g, '&lt;$1$gt;');
console.log(output);
<code></code>

almost 4 years ago · Santiago Trujillo 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