1

I want to create a regular expression that will check if this input starts with letters c or r then any amount of letters or numbers after that is fine here is my code so far:

/^c[a-zA-Z]*/

How would I change this allow either c or r or in capitals C or R at the beginning?

vahdet
  • 6,357
  • 9
  • 51
  • 106
S.Rafiq
  • 63
  • 10

2 Answers2

2

You can use the regex /^[cr][0-9a-z]*$/i for case insensitive match:

var regex = /^[cr][0-9a-z]*$/i;
console.log(regex.test('cat'));
console.log(regex.test('dog'));
console.log(regex.test('Race'));
console.log(regex.test('rat'));
console.log(regex.test('c'));
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

This should work ^([cC]|[rR])\w*$

I created an online playground for you regexr.com/3skji

Johannes
  • 6,490
  • 10
  • 59
  • 108
Appeiron
  • 1,063
  • 7
  • 14