I'm trying to get list of users from string.
EXAMPLE
What I want:
String: "Hello, @alice"
I got users arr: ["alice"]
My code:
export const getRecepientsArrFromMessage = (value: string): string[] | [] => {
if (value) {
const REGEX = /(^@)([\wа-я]|@|.)/gi
return value
.trim()
.split(' ')
.filter((el) => el.length > 0 && REGEX.test(el))
.map((el) => el.slice(1, el.length))
}
return []
}
What I got:
If my string is:
Hello @user1 @user2 @user1@sdf.com @user2 @user1 @user1 @user2
I got users only through one: ['@user1', '@user1@sdf.com', '@user1', '@user2']
What is wrong here?
I tried different variations of the regular expression, but the answer remained the same