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.
Puede usar expresiones regulares /\s*(<.*?>)\s*/g
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);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'));let output = content.replaceAll(/(\s)?(\<\/?\w+\>)(\s)?/g, '$2')Esto debería funcionar