skip to Main Content

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


  1. Pattern:

    const emailAcctName = /^(?!^.)(?!.*.$)[a-zA-Z0-9._-]+$/;
    

    Explanation:

    ^               Start of string
    (?!^.)         Negative lookahead: Do not match if the string starts with a period
    (?!.*.$)       Negative lookahead: Do not match if the string ends with a period
    [a-zA-Z0-9._-]+ Match one or more Latin letters, digits, underscore, hyphen, or period symbols
    $               End of string
    

    Tests:

    const emailAcctName = /^(?!^.)(?!.*.$)[a-zA-Z0-9._-]+$/;
    
    
    console.log(emailAcctName.test("jane_.doe")); // true
    console.log(emailAcctName.test("jane.doe_")); // true
    console.log(emailAcctName.test("jane-doe.")); // false
    console.log(emailAcctName.test("a.b")); // true
    console.log(emailAcctName.test(".jane-doe")); // false
    Login or Signup to reply.
  2. 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 string
    const pattern = /^[a-zA-Z0-9_-](?:[a-zA-Z0-9._-]*[a-zA-Z0-9_-])?$/;
    
    ['f', 'fo', 'foo', 'f.o'].forEach(
      (subject) => {
        console.log(`Match "${subject}": ${pattern.test(subject) ? '✅' : '❌'}`);
      }
    );
    ['.', '.f', 'f.'].forEach(
      (subject) => {
        console.log(`No Match "${subject}": ${!pattern.test(subject) ? '✅' : '❌'}`);
      }
    );
    Login or Signup to reply.
  3. If 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: .+

    ^[a-zA-Z0-9_-]+(?:.[a-zA-Z0-9_-]+)*$
    

    https://regex101.com/r/BLTNib/1

     ^                             # BOL
     [a-zA-Z0-9_-]+                # Required class, one or more
     (?: . [a-zA-Z0-9_-]+ )*      # Optional repeating dot followed by class
     $                             # EOL
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search