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

Answer

(?<=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)
    );
});