0

So basically i want to get the user from a message that has a starting special character like "@" or "#" and push it to the user_lists without the special character only the user.

I know about using regex for matching but im having troubled with pushing every match to an array.

const user_lists = []
const sample_message = '@user and #user2 has a challenge of time management'

My idea was to use includes but it only return as boolean. Please help.

Valid usernames: Without spaces and special character. Just getting all the usernames with a starting @ or #

3
  • read about regex Commented Jun 1, 2021 at 13:18
  • 1
    what are valid user names? Are wild poker, wild-poker or ƒuß valid? Commented Jun 1, 2021 at 13:53
  • valid usernames without space and special characters. Commented Jun 1, 2021 at 14:02

1 Answer 1

2

Try using match()

(?<=@|#)\w+

Regex Demo

const sample_message = '@user and #user2 has a challenge of time management'

const user_lists = sample_message.match(/(?<=@|#)\w+/g) || []

console.log(user_lists)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.