1

How can I extract date from a string where date format is dd-mmm-yyyy (example 01-dec-2020) in Javascript using regular expression.

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Ambady Ajay
  • 175
  • 1
  • 10
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. Also see https://idownvotedbecau.se/noattempt/ – Samathingamajig Dec 02 '20 at 05:07
  • ok,will do that. – Ambady Ajay Dec 02 '20 at 05:42
  • 1
    Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Beyazid Dec 02 '20 at 13:29

2 Answers2

3

You can simply use this regex: \d{2}-[A-Za-z]{3}-\d{4}

let str = 'something 01-dec-2020 something';

let result = str.match(/\d{2}-[A-Za-z]{3}-\d{4}/);

console.log(result);
dhruw lalan
  • 791
  • 1
  • 11
  • 23
1

If you want to spell out the exact months, here is the regex I came up with:

var dates = text.match(/\d{2}-(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)-\d{4}/gi);

Fiddle, for example, which outputs to the console:

https://jsfiddle.net/sok0u9mb/

Tore
  • 1,236
  • 2
  • 11
  • 21