使用jQuery和Regexp更改日期字符串

使用jQuery和Regexp更改日期字符串,jquery,regex,date,replace,Jquery,Regex,Date,Replace,我应该使用哪种代码,以便jQuery查找页面上的每个YYYY-MM-DD格式(纯文本)字符串,并用DD-MM-YYYY替换它 非常感谢 这可能有用: $(document.body).contents().each(function(i,e) { $(e).text(function(i,text) { return text.replace(/(\d+)-(\d+)-(\d+)/g, function($full, $year, $month, $day) {

我应该使用哪种代码,以便jQuery查找页面上的每个YYYY-MM-DD格式(纯文本)字符串,并用DD-MM-YYYY替换它

非常感谢

这可能有用:

$(document.body).contents().each(function(i,e) {
    $(e).text(function(i,text) {
        return text.replace(/(\d+)-(\d+)-(\d+)/g, function($full, $year, $month, $day) {
            return [$day, $month, $year].join('-');
        });
    });
});

示例:

我强烈建议使用可识别的元素包含日期,而不是相信(任何)JavaScript能够可靠地解析和转换有意义的纯文本字符串。非常感谢!工作得很好!