How To Filtering User Mentions from Webhook Data?

Describe the problem/error/question

Hello everyone, I have a question. I received a webhook that includes tagged notes that look like this: “@username text”. After receiving this, I send “@username” to Slack to tag that user. My question is, how do I filter only the instances of “@username” that contain the “@” symbol in the word?

Please share your workflow

Share the output returned by the last node

Information on your n8n setup

  • n8n version:1.35.0
  • Database (default: SQLite):SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main):default
  • Running n8n via (Docker, npm, n8n cloud, desktop app):docker-compose
  • Operating system:linux

The simplest solution that I can think of is using the IF node with a ‘string starts with’ condition. If that’s not robust enough, you could also use an IF node with a regex match to ensure the @ symbol is followed by a name. And one step further could be validating the usernames against a list of know ones - you could use a code node for that.

@bartv Hey, thank you for the reply. Don’t you think using ‘contains’ instead of ‘starts with’ would be more effective? For example, if my note looks like this: ‘test @username1 test @username2’, using ‘starts with’ would lead to a false result. Anyways, is there any Java code to extract a word that has ‘@’ in front? I think that would suit me well instead of validating a list of usernames in the next step.

Sure! You defined your input as ‘@username text’ though, so I worked with that :slight_smile:

const input = $input.first().json.payload;
const regex = /(?<!\w)@\w+/g;
const match = input.match(regex);

return { match };
1 Like

@bartv It worked thank a lot.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.