Search code examples
regexre2

Regex for strings not starting with "My" or "By"


I need Regex which matches when my string does not start with "MY" and "BY".

I have tried something like:

r = /^my&&^by/

but it doesn't work for me

eg

mycountry = false ; byyou = false ; xyz = true ;


Solution

  • You could test if the string does not start with by or my, case insensitive.

    var r = /^(?!by|my)/i;
    
    console.log(r.test('My try'));
    console.log(r.test('Banana'));

    without !

    var r = /^([^bm][^y]|[bm][^y]|[^bm][y])/i;
    
    console.log(r.test('My try'));
    console.log(r.test('Banana'));
    console.log(r.test('xyz'));