-2

i need a regex that fits these conditions

  • may contain letters and numbers. numbers are optional but must contain at least 1 letter
  • at least 2 characters
  • can contain ONLY the "-" character of special characters. this is optional
  • must begin with a letter
  • no whitespace
  • no turkish characters

How should i create a regex query according to these conditions?

pneuma
  • 71
  • 5

1 Answers1

0

I would create a function like so:

function isGood(string){
  return /^[a-z][a-z0-9-]+$/i.test(string);
}
console.log(isGood('This should return false'))
console.log(isGood('@no'));
console.log(isGood('Nice'));
console.log(isGood('a'));
console.log(isGood('a!'));
console.log(isGood('great'));
console.log(isGood('This-should-also-pass-the-test'));
StackSlave
  • 10,198
  • 2
  • 15
  • 30