Regex not required. A combination of split()
and slice()
will do as well:
var myurl = "https://example.com/display/~test/2010/07/06/Day+2.+Test+Page";
var parts = myurl.split("/"); // ["https:", "", "example.com", "display", "~test", "2010", "07", "06", "Day+2.+Test+Page"]
var ymd = myurl.slice(5,8); // ["2010", "07", "06"]
var date = new Date(ymd); // Tue Jul 06 2010 00:00:00 GMT+0200 (W. Europe Daylight Time)
There are several comprehensive date formatting libraries, I suggest you take one of those and do not try to roll your own.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…