I wanted to validate the Name data files with following condition
- the Name contains digits 0 to 9, not all numeric
- Name can contain hyphen -, provided that it is not the first or last character of the name
- Name must contain 2 character, rest char are optional
So I tried the following Regex
const regExp = /^[a-zA-Z0-9]+[a-zA-Z_-]+[a-zA-Z0-9]$/;
I need at least two characters, remaining are optional in the regExp
everything works fine except this [a-zA-Z0-9] in the last, I want to make this optional
/^[a-zA-Z0-9][a-zA-Z_-]*[a-zA-Z0-9]$//^(?=.{2})(?!\d+$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+)*$/2-2a valid name? If yes, then @WiktorStribiżew has the solution./^(?=.{2})(?![^a-zA-Z]+$)[a-zA-Z0-9]+(?:[_-][a-zA-Z0-9]+)*$/