Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda