I need a regular expression that can contain Latin letters, digits, the underscore (_), the minus (-), and the point (.) symbols but the point symbol cannot be used as the first or the last symbol. I believe I need to use some sort of quantifier because the pattern I have now only works with 3 or more characters. I need it to work even with just one character, it just can’t be the (.)point symbol. See my current pattern below:
const emailAcctName = /^[a-zA-Z0-9_-]+[a-zA-Z0-9._-]+[a-zA-Z0-9_-]$/;
3
Answers
Pattern:
Explanation:
Tests:
Match the first character, followed by an optional match of any amount of the inner characters and a single last character.
/^[a-zA-Z0-9_-](?:[a-zA-Z0-9._-]*[a-zA-Z0-9_-])?$/
^
matches the start of the string[a-zA-Z0-9_-]
matches one or more Latin letter, digit, underscore or minus symbol[a-zA-Z0-9_.-]
matches one or more Latin letter, digit, underscore or minus symbol and the dot(?:[a-zA-Z0-9._-]*[a-zA-Z0-9_-])?
matches zero or one repetitions of a non-capturing group that contains any amount of the specified characters (including the dot) followed by a single occurrence of the specified characters (excluding the dot)$
matches the end of the stringIf the dot is only allowed in the middle, you can just sandwich the dot between
[a-zA-Z0-9_-]
‘s.If you want to allow dots side by side, add a quantifier:
.+
https://regex101.com/r/BLTNib/1