Reputation: 235
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
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
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