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

197
Views
Regex for detecting tagged @users

I am making an app where if a user has "@username" in a text, it tags them and sends notification. I tried to doing this by using this regex /\B(\@[a-zA-Z]+\b)(?!;)/ this works but I dont want it to consider it valid when there is more than one "@". For example:

@username - valid

@@username - invalid

hey @username - valid

@usern@name - invalid

hey@username - invalid

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can use regex ^@[a-z]+$

enter image description here

function isValid(str) {
  const regex = /^@[a-z]+$/;
  return str.split(" ").some((s) => regex.test(s));
}

console.log(isValid("@username"))
console.log(isValid("hey @username"))
console.log(isValid("@@username"))
console.log(isValid("@usern@name"))
console.log(isValid("hey@username"))

about 4 years ago · Juan Pablo Isaza Report

0

(?<=\s|^\s?)@[a-zA-Z]+(?=\s|\s?$) might work.

const testcase = ['@username', '@@username', 'hey @username', '@usern@name', 'hey@username', 'hey @username hey', '@username hey'];

testcase.forEach(text => {
    console.log(text + ' => ' + 
        /(?<=\s|^\s?)@[a-zA-Z]+(?=\s|\s?$)/.test(text)
    );
});

about 4 years ago · Juan Pablo Isaza Report

0

Regex that will help you acheive the task you want -

^@[A-z0-9_-.]+$

Assuming that you will also encounter usernames such as -

@user.name

@user-name

@user_name

@user.name.

@User_name

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!