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

175
Vistas
How to remove leading and trailing space in a tag? regex

I have user-defined tags in the content, and have keyword tags that has corresponding id number on it

const tags = ["<i>", "<c>", "<b1>", "<b2>", "<b3>"];
keyword tags e.g. "<key2>", "<key10>"

I have to remove the leading and trailing spaces, because I need to split it by word.


Here's is my sample content:

let content = 
`<b1> <c> "The Modern Amphibians" </c> </b1>
Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
Don’t think that this term’s work will be <key23> a piece of cake </key23>`

The expected output would be (leading & trailing space(s) removed)

let output = 
`<b1><c>"The Modern Amphibians"</c></b1>
Modern <b>amphibians</b> have a simplified <key2>anatomy</key2> compared to their ancestors due to <i>paedomorphosis</i>. 
Caused by two evolutionary trends: <b2>miniaturization</b2> and an unusually.
Don’t think that this term’s work will be <key23>a piece of cake</key23>`

I have tried to make my own regex, starting with the c tag but I'm not sure if this is proper, since I just need only to remove the spaces, but my regex is including the tag.

const customRegex = \((<c>\s)|(\s<\/c>))\g.

Someone can help. thanks.

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

0

You can use regex /\s*(<.*?>)\s*/g

enter image description here

let content = `<b1> <c> "The Modern Amphibians" </c> </b1>
Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
Don’t think that this term’s work will be <key23> a piece of cake </key23>`;

const result = content.replace(/\s*(<.*?>)\s*/g, "$1");
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You may try (<[^<>\/]+>)\s+|\s+(<\/[^<>]+>).

This ensures to only remove spaces either after an opening tag (e.g. <s>) or before a closing tag (e.g. </s>)

const regex = /(<[^<>\/]+>)\s+|\s+(<\/[^<>]+>)/g;

const content = 
`<b1> <c> "The Modern Amphibians" </c> </b1>
Modern <b>amphibians </b> have a simplified <key2>anatomy </key2> compared to their ancestors due to <i>  paedomorphosis</i>. 
Caused by two evolutionary trends: <b2> miniaturization  </b2> and an unusually.
Don’t think that this term’s work will be <key23> a piece of cake </key23>`;

console.log(content.replace(regex, '$1$2'));

about 4 years ago · Juan Pablo Isaza Denunciar

0

let output = content.replaceAll(/(\s)?(\<\/?\w+\>)(\s)?/g, '$2')

This should work

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