Capture dates of a text using regular expression in javascript

Asked

Viewed 6,608 times

2

I have a function that returns data in JSON and places in certain places with jQuery. One of these data is a text that contains a period of dates (Beginning and end).

Example of returned text:

ESPÍRITO SANTO - State: This is how much the Brazilian has paid of taxes in the period from 01/01/2014 to 11/03/2014

In the above case I want to return only these two dates, 01/01/2014 and 11/03/2014.

I have seen several examples in javascript of capturing texts between defined characters, but not of capturing a text format (in the case of a date) and returning only that there.

So is there any way I can return only these two dates? What function should I use?

  • The format is always dd/mm/aaaa?

5 answers

3


Use regular expressions.

var datas = texto.match(/\b(\d+\/\d+\/\d+)\b/g);
console.log(datas); // output: [ "01/01/2014", "11/03/2014" ]

In this particular case you take all strings that contain three numerical sequences separated by a bar (/).

  • It worked. Thank you..

2

A regular expression to retrieve dates in the formed dd/mm/aaaa is \d{2}\/\d{2}\/\d{4}.

In javascript, you can create the regular expression and then use the function exec to iterate on the items found;

Example:

var pat = /\d{2}\/\d{2}\/\d{4}/g;
var resultados = [];
var item;
while (item = pat.exec(str)) {
    resultados.push(item[0]);
}

Occurrences would be in the array resultados.

And by encapsulating everything in one function we can do so:

function getDatas(str) {
    var pat = /\d{2}\/\d{2}\/\d{4}/g;
    var resultados = [];
    var item;
    while (item = pat.exec(str)) {
        resultados.push(item[0]);
    }
    return resultados
}

See the example in jsfiddle

(do not forget to open your browser console to see the output)

1

var reg = /([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})/g;
var str = "Um texto com uma data aqui 01/01/2012 e mais uma aqui 03/04/2000";
var todasAsDatas = str.match(reg);

It is important to remember that the g at the end of the regular expression specifies that all occurrences must be returned.

This expression allows dates such as 1/9/2013, or 01/09/2013.

0

I think it’s important to limit the wrong expressions a little. In the expression below only I do not restrict the differences of days per month and leap year, nor do I limit a minimum starting year.

var reg = /(([0-2]{1}[0-9]{1}|3[0-1]{1})\/(0[0-9]{1}|1[0-2]{1})\/[0-9]{4})/g;
var str = "Um texto com uma data aqui 01/01/2012 mas essa data errada aqui não pegaria 32/13/2000";
var todasAsDatas = str.match(reg);
  • It follows a more complete version, trot number of days of the months.... only February that stayed with 29 days always.... Year also only accepted from 1960 up. 

((((0[1-9]|[1-2][0-9]|3[0-1])/(01|03|05|07|08|10|12))|((0[1-9]|[1-2][0-9]|30)/(04|06|09|11))|((0[1-9]|[1-2][0-9])/(02)))/(19[6-9][0-9]|2[0-9]{3}))

0

If the format is always dd/mm/aaaa, regular expression is very simple:

\d{2}\/\d{2}\/\d{4}

Online test

Browser other questions tagged

You are not signed in. Login or sign up in order to post.