Home > Net >  How would I get a discord tag from a string with regex?
How would I get a discord tag from a string with regex?

Time:01-02

I don't have basically any experience when it comes to regex, and have a basic understanding of string manipulation. I am trying to separate a discord tag from the rest of a string, but am running into issues.

For example, I was able to separate a tag like vunsh#3191 from 'vunsh#3191 has done blah blah blah`. But when a discord tag has a space in it or other characters, it doesn't detect it with the system I'm using right now.

Ex. My current system would get just vunsh's from vunsh's discord#1234 blah blah blah. I need vunsh's discord#1234

I found a method called string#match(), and it seems to be what I am looking for. What I am struggling with is what to put as the regex pattern that passes into #match().

Ex.

const paragraph = 'vunsh thing#2134 blah blah';
const regex = //what goes here?
const found = paragraph.match(regex);

console.log(found);
// expected output: Array ["vunsh thing#2134"]

Note: Not all the strings I am working with contain the word "vunsh", but all discord tags have a # and 4 numbers right after that.

CodePudding user response:

You could use

const regex = /.*#\d{4}\b/g;

But this wont work in all cases where theres text before the username, for that there is no solution.

  • Related