简体   繁体   中英

I have an problem at hand on regular expressions in Javascript

var pattern = /[AZ][az]{2}-\d{2}-\d{4}/ This is the pattern, I have used to match the date string of format Jan-12-2000

Problem is I need the pattern for following strings,

  • Jul 23, 2020
  • Jul 23
  • Jul 2020
  • 23, 2020

I am really stuck at this trying so many options not getting exact result. Really appreciate if somebody could provide the solution.

 const rgx = /([AZ][az]{2,3}[-\s,])?\d{2,4}([-\s,]\d{4})?/; console.log(rgx.test('Jan-12-2000')) console.log(rgx.test('Jul 23, 2020')) console.log(rgx.test('Jul 23')) console.log(rgx.test('Jul 2020')) console.log(rgx.test('23, 2020'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM