Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
¿Cómo eliminar el espacio inicial y final en una etiqueta? expresiones regulares

Tengo etiquetas definidas por el usuario en el contenido y tengo etiquetas de palabras clave que tienen el número de identificación correspondiente.

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

Tengo que eliminar los espacios iniciales y finales porque necesito dividirlo por palabra.


Aquí está mi contenido de muestra:

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

El resultado esperado sería (los espacios iniciales y finales eliminados)

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

He intentado hacer mi propia expresión regular, comenzando con la etiqueta c , pero no estoy seguro de si esto es correcto, ya que solo necesito eliminar los espacios, pero mi expresión regular incluye la etiqueta.

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

Alguien puede ayudar. Gracias.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar expresiones regulares /\s*(<.*?>)\s*/g

ingrese la descripción de la imagen aquí

 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 Report

0

Puede probar (<[^<>\/]+>)\s+|\s+(<\/[^<>]+>) .

Esto garantiza que solo se eliminen los espacios después de una etiqueta de apertura (p. ej. <s> ) o antes de una etiqueta de cierre (p. ej </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 Report

0

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

Esto debería funcionar

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!