Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Date and Time from string with jQuery

I have a string like asdasd 18.06.2013 07:57 the following code return the date and time (18.06.2013 07:57) well, but i hope there is more clean solution to do this

report_t=$('#reportResult h2:nth(0)').text().match(/[\d]+/g);
$('#output').val(function(){
    return report_t[0]+"."+report_t[1]+"."+report_t[2]+" "+report_t[3]+":"+report_t[4];
}

There is a clean solution to do that!?

UPDATE I found a solution:

/[0-9].+/g

and Jack has one too. Thak You!

/\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}/g
like image 204
eapo Avatar asked Feb 05 '26 18:02

eapo


1 Answers

Just move the matching inside the expression:

/\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}/g

Then, each match will contain the whole date/time block.

> 'asdasd 18.06.2013 07:57'.match(/\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}/g)
["18.06.2013 07:57"]
like image 146
Ja͢ck Avatar answered Feb 07 '26 09:02

Ja͢ck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!