Ambady Ajay
Ambady Ajay

Reputation: 235

How to extract date from a string of format dd-mmm-yyyy in js

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

Upvotes: 1

Views: 444

Answers (2)

dhruw lalan
dhruw lalan

Reputation: 801

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);

Upvotes: 3

Tore
Tore

Reputation: 1264

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/

Upvotes: 1

Related Questions