1

I am trying to use this expression:

var reg = "/(jan|feb|mar)[A-z]*\[0-9]/"

to capture at least the first three letters of the month(or more letters) plus a digit. This does not work however. When I remove the parenthesis, it works but then the [A-z]*[0-9] bit only aplies to march. Please help, thanks.

George Salamanca
  • 180
  • 1
  • 3
  • 11

2 Answers2

0

Your regex is incorrect, also the regex should not be a string.

Use regex /(jan|feb|mar)[a-z]*[0-9]/i

Regex explanation: https://regex101.com/r/9Qv2dy/2

Snippet:

var reg = /(jan|feb|mar)[a-z]*[0-9]/i;

console.log('January1'.match(reg));
Rahul Desai
  • 15,242
  • 19
  • 83
  • 138
0

Your code contains several issues.

  1. The /.../ regex literal should not be put inside quotes.
  2. [A-z] matches more than just letters, you need [A-Za-z]
  3. A \[ pattern matches a literal [ char. To match a digit, you need [0-9] or \d. To match 1 or more digits: [0-9]+ or \d+.

Use

var reg = /(?:jan|feb|mar)[a-z]*[0-9]/i;

See JS demo:

var reg = /(?:jan|feb|mar)[a-z]*[0-9]/i;
console.log("Date: January1".match(reg));
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563