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

163
Vistas
javascript, substring should exclude html tags in its character count

The substr should not consider html tags in its count, is there any way to achieve it?

for example, if I have

 input =  <p> This is testing <strong> hi </strong> how are you, Well doing  </p>

The input.substring(0, 20) should not consider <p> and <strong> tags in its character count.

The html tags should not be removed from the string, just they have to be ignore in the count for substring.

     <h2 v-html="this.$options.filters.limitText(input)" ></h2>

     filters: {
      limitText: function (val) {
       if(val && (val.length > 20)) {
         return (val.substring(0, 20).replace(/\s\S*$/, '') + ' ...')
       }
      return val;
    
      }
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Does this help:

const input = "<p> This is testing <strong> hi </strong> how are you, Well doing  </p>";
const tags = /<[^>]*>/g;
const output = input.replace(tags, '').substring(0, 20);
console.log(output); //-> ' This is testing  hi'
about 4 years ago · Juan Pablo Isaza Denunciar

0

My suggestion:

const input = "<p> This is testing <strong> hi </strong> how are you, Well doing  </p>";
const count = input.replace(/(<([^>]+)>)/gi, "").length;

console.log(count); // Output: 47
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