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

182
Visualizações
Regex for hashtags is returning not correct number js

I am trying to get the number of hashtags my case is for instagram, wanna make a validation but for some reason the number is appearing 2 instead of 3, I have 3 hashtags there! Do you guys know why?

let text = "#Visit #test  #test2";
let pattern = /(^|\W)(#[a-z\d][\w-]*)/g;
let result = (text.match(pattern)).length;

console.log(result)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need to add the case-insensitive modifier to your regex.

let text = "#Visit #test  #test2";
let pattern = /(^|\W)(#[a-z\d][\w-]*)/ig; // Added 'i' as the modifier
let result = text.match(pattern).length;

console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

You could do it in another way :

let text = "#Visit #test  #test2"
let result= text.split("#").length - 1

console.log(result)

Here you split the text at the hash character, therefore the length will be the number of hashtags - 1.

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. By using regex :

let text = "#Visit #test  #test2";

var count = (text.match(/#/g) || []).length;

console.log(count);

  1. By using String.split() method :

let text = "#Visit #test  #test2";

console.log(text.split('#').length - 1);

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