1

I want to find Dates in a document.

And return this Dates in an array.

Lets suppose I have this text:

On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994

Now my code should return ['03/09/2015','27-03-1994'] or simply two Date objects in an array.

My idea was to solve this problem with regex, but the method search() only returns one result and with test() I only can test a string!

How would you try to solve it? Espacially when you dont know the exact format of the Date? Thanks

John Smith
  • 6,105
  • 16
  • 58
  • 109

1 Answers1

9

You can use match() with regex /\d{2}([\/.-])\d{2}\1\d{4}/g

var str = 'On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994';

var res = str.match(/\d{2}([\/.-])\d{2}\1\d{4}/g);

document.getElementById('out').value = res;
<input id="out">

Regular expression visualization

Or you can do something like this with help of capturing group

var str = 'On the 03/09/2015 I am swiming in a pool, that was build on the 27-03-1994';

var res = str.match(/\d{2}(\D)\d{2}\1\d{4}/g);

document.getElementById('out').value = res;
<input id="out">

Regular expression visualization

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188