Regex for 2 words, non-numeric, and one space

I am trying to build a javascript regex that allows users to enter 2 words only, non-numeric, and only one space between them. I have been trying this one: ^\w+\s*\w+ ?$ but don’t know how to avoid numbers.

24 thoughts on “Regex for 2 words, non-numeric, and one space”

  1. If the 2 words only use the alphabets without any special characters.

    You can use the simplest one. /^([a-z]+)\s+([a-z]+)(\s*)$/gi

     [a-z]+ tells any character in the alphabet with quantifier of one or more
     \s* -> is a space with a quantifier of zero or more
      i -> on the last part tells case-insensitive matching
    
    let a = 'my Word';
    let b = 'oneWord';
    let c = 'using three words';
    let d = 'onewordwithspace ';
    let e = 'my wordwithSpace ';
    
    let regEx = /^([a-z]+)\s+([a-z]+)(\s*)$/gi;
    
    console.log((regEx.test(a))); // true
    console.log((regEx.test(b))); // false
    console.log((regEx.test(c))); // false
    console.log((regEx.test(d))); // false
    console.log((regEx.test(e))); // true
    Reply

Leave a Comment