Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!