Make a regex to remove characters

2

In a return of a jquery function, the returned date comes in this format.

/Date(1401731794773)/

I would like to remove the invalid characters on this date, which are:

/Date( and )/

I only need the date (remaining) component to compose a valid date. So how do I do this? I think a REGEX would be the best way, right?

    
asked by anonymous 16.06.2014 / 18:29

4 answers

6

Regex in this case is overkill ... Simply use substring :

var entrada = "/Date(1401731794773)/";
var conteudo = entrada.substring(6, entrada.length-2);

This is possible in this case because both the prefix and the suffix have a fixed size: "/Date(".length == 6 and ")/".length == 2 .

Note: My original response assumed that the code was on the server side, in C #. Anyway, the solution would be the same:

string entrada = "/Date(1401731794773)/";
string conteudo = entrada.Substring(6, entrada.length-2);
    
16.06.2014 / 18:47
1

With regular expressions you can do this:

var resultado = "/Date(1401731794773)/"

data = resultado.match(/[\d]+/);
console.log(data); // 1401731794773

The expression /[\d]+/ will cause only numbers to be captured.

It's also possible to be doing this:

var resultado = "/Date(1401731794773)/"

data = resultado.replace(/[^\d]+/g, "");
console.log(data); // 1401731794773

The expression /[^\d]+/g causes all non-numeric characters to be matched, which in this case, Date() so simply make the exchange by calling replace() .

    
16.06.2014 / 19:47
0

I solved it like this:

var dtCadastro = data.agendamento.DataCadastro.substring(6, data.agendamento.DataCadastro.length - 2);
var dtAgendanmento = data.agendamento.DataAgendamento(6, data.agendamento.DataAgendamento.length - 2);
var dtVisita = data.agendamento.DataVisita(6, data.agendamento.DataVisita.length - 2);
    
16.06.2014 / 18:58
0

The most effective way in javascript is:

var val = "/Date(1402369200000)/";
var re = "/-?\d+/";
console.log(re.exec(val));

// output: ["1402369200000"]
    
16.06.2014 / 19:37