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

180
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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