Twitch channel name RegEx

Hello !

I would like to get an “official” regular expression used to validate the channel/user names.
Can I find it anywhere ? Or is it at least possible to get a complete description of the rules used to validate an username ?

Thank you in advance !

A twitch channel name has a very simple regex.
First. a channel always starts with a “#” in IRC, but not on twitch itself, so try to find your usecase.

FOR irc you would first check for the # :
/^#

Good, we got that. if you don’t need it for irc , you can skip that one.

Okay, so what’s next… a twitch name exist of upper/lowercase letters and numbers.
twitch has a minimum length.

[a-zA-Z0-9] would describe all letters/numbers.

let’s say a name has a minimum of 4 characters (not sure)

Okay so 4 characters:
[a-zA-Z0-9]{4,}
In here {4,} describes 4, up to “a lot”. but twitch probably has a maximum so let’s make that {4,25}

Now to finish it up simply add:
$/
to end the regex.

result:
/^(#)?[a-zA-Z0-9]{4,25}$/

Note that I made the # optional, you can keep mandatory or remove it for your usecase.

EDIT:
max length is 25. I just checked. updated script

If you are referring to validating a username, I suggest you try to register a new username in the extremes of short and long etc until you get the actual parameters. The register page might even have the limits enforced by javascript, so you can check there as well.

I believe the username can consist of letters, numbers and sometimes spaces (signified by \s in IRC)

@JB940, two additions to your regex:

  1. There’re some usernames with only 3 characters (e.g. Orb)

  2. The channel/username cannot start with an underscore

    /^(#)?[a-zA-Z0-9][\w]{2,24}$/

Edit: changed [a-zA-Z0-9_] to [\w] since it’s the same and shorter :stuck_out_tongue:

1 Like

Thanks !

I found the same answer except that I didn’t know the min and max length of a username.
Are you sure that the minimum is 3 ? (the shortest name I know is ESL so …)

3 character names were prizes for some Twitch contests/events as I understand it. They haven’t do that for some time now but there are a few out there.

Ah thanks. I didn’t consider the underscore, don’t see many peopel with that name :^)

If you’re trying to make a regex for new usernames, 4 characters is the minimum. If you’re validating existing usernames, some users only have 1 character names. And if you’re validating display names then some people have spaces in their name and are hundreds of characters long.

1 Like

Thanks :smile:

I’m validating existing usernames.

I think I have everything I need now.

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