You can use this way: /^\[[\w][\w\-]*[\w]+\]$/
( online example )
What regex does:
-
^
indicates the beginning of the string
-
\[
indicates the own character [
, escaped not to be interpreted as list
-
[\w]+
accepts numbers and letters. One or more times
-
[\w\-]
the same as the previous plus the characters _
and -
, zero or multiple times.
-
[\w]+
I put this class back but without \-
to ensure that the last character before the ]
end is not a -
.
-
\]
indicates the character ]
-
$
indicates end of string
If you want to prevent it from having only numbers and the -
separator you can check with negative lookahead
which in the background is another regex with what is not allowed. In this case it would look like this: /^(?!^\[[\d\-]+\]$)\[[\w][\w\-]*[\w]+\]$/
, where (?!xxx)
is what indicates this negative check.
( online example )