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

221
Vistas
How to remove HTML tag (not a specific tag ) with content from a string in javascript

Is there any way to strip HMTL tag with content in HTML

example :

const regexForStripHTML = /(<([^>]+)>)/gi
const text = "OCEP <sup>&reg;</sup> water product"
const stripContent = text.replaceAll(regexForStripHTML, '')

output : 'OCEP &reg; water product'

I need to remove &reg; also from a string

expected output
OCEP water product

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

0

This should suffice your use-case:

const regexForStripHTML = /<sup.*>.*?<\/sup>/ig
const text = "OCEP <sup>&reg;</sup> water product"
const stripContent = text.replaceAll(regexForStripHTML, '');

console.log(stripContent);
If you want to do it with any HTML tag. See code below:

const regexForStripHTML = /<.*>.*?/ig
const text = "OCEP <html>&reg;</html> water product"
const stripContent = text.replaceAll(regexForStripHTML, '');

console.log(stripContent);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Context

To remove the text from between the tags you would need to match opening and closing tags of the same tag name. This regex would match the starting tags <(?<tagname>.*?)>. Notice how tagname remembers the and is being used for the regex part of the corresponding closing tags which is <\/\k<tagname>> the part in between .*? is to match for any text.

Code

const regexForStripHTML = /(<(?<tagname>.*?)>.*?<\/\k<tagname>>)/g
const text = "OCEP <sup>&reg;</sup> water product"
const stripContent = text.replaceAll(regexForStripHTML, '$')

Note

I haven't thought about what happens if the tags are nested.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Removing all HTML tags and the innerText can be done with the following snippet. The Regexp captures the opening tag's name, then matches all content between the opening and closing tags, then uses the captured tag name to match the closing tag.

const regexForStripHTML = /<([^</> ]+)[^<>]*?>[^<>]*?<\/\1> */gi;
const text = "OCEP <sup>&reg;</sup> water product";
const stripContent = text.replaceAll(regexForStripHTML, '');
console.log(text);
console.log(stripContent);

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