-3

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 ;

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Ashutosh Jha
  • 15,451
  • 11
  • 52
  • 85

3 Answers3

4

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'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

if you are only concerned with only specific text at the start of the string than you can use latest js string method .startsWith

  let str = "mylove";

  if(str.startsWith('my') || str.startsWith('by')) {
    // handle this case
  }
xkeshav
  • 53,360
  • 44
  • 177
  • 245
-1

Try This(Regex is NOT case sensitive):

  var r = /^([^bm][y])/i; //remove 'i' for case sensitive("by" or "my")

console.log('mycountry = '+r.test('mycountry'));
console.log('byyou= '+r.test('byyou'));
console.log('xyz= '+r.test('xyz'));

console.log('Mycountry = '+r.test('Mycountry '));
console.log('Byyou= '+r.test('Byyou'));

console.log('MYcountry = '+r.test('MYcountry '));
console.log('BYyou= '+r.test('BYyou'));
One Man Crew
  • 9,420
  • 2
  • 42
  • 51