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

88
Vistas
Escape de etiquetas personalizadas excepto etiquetas html

Quiero escapar de las etiquetas personalizadas, excepto las etiquetas HTML como fuerte, negrita, cursiva.

 Input: "Hello World! <notification>Name</notification><nat>Nat tag</nat> <strong>This should be strong</strong><nas>Nas Tag</nas>" Output: Hello World! <notification>Name</notification> <nat>Nat tag</nat>**This should be strong**<nas> Nas Tag</nas>

cadena.reemplazar(/</g, "<").reemplazar(/>/g, ">") .reemplazar(/"/g, """).reemplazar(/'/g, "'")
.replace(/<(?!/?strong>)[^>]+>/g, '')

Probé con el reemplazo anterior, pero también está reemplazando <strong> with &lt; strong &gt; Cualquier ayuda sería apreciada.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Es mejor tener una lista blanca de etiquetas permitidas y "escapar" cualquier cosa que no esté en la lista. Algo como esto funcionará para una implementación simple, pero en general, regex no es una buena herramienta para analizar HTML .

 var input = "Hello World! <notification asdfasd=asd>Name</notification><nat>Nat tag</nat> <strong>This should be strong</strong><nas>Nas Tag</nas>" var output = escapeCustomTags(input, ['strong']) console.log(output); function escapeCustomTags(input, allowed_tags = []) { // Make allowed tags array lower case allowed_tags = allowed_tags.map(c => c.toLowerCase()); // Output is the input, edited var output = input; // Attempt to match an opening or closing HTML tag var reg = /<\/?([a-zA_Z0-9]*)[^>]*?>/g; // An array that will contain all disallowed tags var disallowed_tags = []; // For each tag in the input, if it's allowed, skip // Else, add it to the array. var match; while ((match = reg.exec(input)) !== null) { if (allowed_tags.includes(match[1].toLowerCase())) continue; disallowed_tags.push(match[0]); } // Replace each disallowed tag with the "escaped" version disallowed_tags.forEach(tag => { var find = tag; var replace = tag.replace('<', '&lt;').replace('>', '&gt;'); output = output.replace(find, replace) }); return output; }

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