Regex that captures data entered by user, typed in a phrase

4

If I have a sentence: "melhor 01/01/2016 - 05/01/2016" How can I retrieve the dates of this sentence using regex?  thus: var data1="01/01/2016" , data2="05/01/2016"

    
asked by anonymous 18.05.2016 / 21:05

1 answer

5

You can do this by using the String.match method. You need to pass a regular expression that, when evaluated, will return a array containing the values that hit the last expression.

Example:

var regex_date = /(0[1-9]|[1-2]\d{1}|3[01])\/(0[1-9]|1[1-2])\/\d{4}/g

var dates = 'Vamos sair em 04/02/2015 e voltar em 04/05/2015'.match(regex_date)

for (date in dates) {
  document.write(dates[date] + "<br>")

}
    
18.05.2016 / 21:08