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
  • 5,611
  • 9
  • 42
  • 94
S.Rafiq
  • 63
  • 9

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
  • 29,658
  • 5
  • 35
  • 59
0

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

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

Johannes
  • 6,105
  • 6
  • 54
  • 94
Appeiron
  • 1,053
  • 7
  • 14