Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否有一种更有效的方法来匹配/替换HTML表中的日期?_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 是否有一种更有效的方法来匹配/替换HTML表中的日期?

Javascript 是否有一种更有效的方法来匹配/替换HTML表中的日期?,javascript,jquery,regex,Javascript,Jquery,Regex,我的任务是统一包含不同数据库的HTML表的日期格式。使用SQL过程不是一个选项。已知的事实是,TBODY中的单元格将是纯文本或纯文本包装在一个链接中 我写了一个jQuery插件来完成这项工作,但我想知道做innerHTML与循环每个TD是否有意义 原始插件: $.fn.reformatAllDates = function () { var $txt = $(this).text(); var reformatDate = function ($str) { var $part

我的任务是统一包含不同数据库的HTML表的日期格式。使用SQL过程不是一个选项。已知的事实是,TBODY中的单元格将是纯文本或纯文本包装在一个链接中

我写了一个jQuery插件来完成这项工作,但我想知道做innerHTML与循环每个TD是否有意义

原始插件:

$.fn.reformatAllDates = function () {
  var $txt = $(this).text();

  var reformatDate = function ($str) {
    var $parts = $str.split('/'),
        $year = $parts[2],
        $month = $parts[0],
        $day = $parts[1];
    if ($parts.length === 3) {
      $month = $month.length < 2 ? '0' + $month : $month;
      $day = $day.length < 2 ? '0' + $day : $day;

      //Returns dates in sort friendly format [YYYY-MM-DD]
      return $year + '-' + $month + '-' + $day;
    }
    return $str;
  };

  var $result, $reg = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);

  while (($result = $reg.exec($txt)) !== null) {
    var $match = $reg.exec($txt)[0];
    var $newFormat = reformatDate($match);
    $txt = $txt.replace($match, $newFormat);
  }
  return $(this).html($txt);
}
innerHTML实现:

$('table.display tbody tr td').each(function () {
  if ($(this).html().indexOf('href') >= 0)
    $(this).find('a:first').reformatAllDates();
  else
    $(this).reformatAllDates();
});
$('table.display tbody').reformatAllDates();
这是可行的,尽管在innerHTML失败之前,我还没有测试它有多大,并在中准备了回退步骤

$.fn.reformatAllDates = function () {
  var $result, 
      $reg = new RegExp(/\d{1,2}\/\d{1,2}\/\d{4}/),
      $r2 = new RegExp(/[\n\r\f]/g),
      $html = $(this).html();
  $html = $html.replace($r2,'');
  $html = $html.replace($r2,'');

  var reformatDate = function ($str) {
    var $parts = $str.split('/'),
        $year = $parts[2],
        $month = $parts[0],
        $day = $parts[1];
    if ($parts.length === 3) {
      $month = $month.length < 2 ? '0' + $month : $month;
      $day = $day.length < 2 ? '0' + $day : $day;
      return $year + '-' + $month + '-' + $day;
    }
    return $str;
  };

  var $match, $newFormat, $msg;
  while (($result = $reg.exec($html)) !== null) {
    $match = $reg.exec($html)[0];
    $newFormat = reformatDate($match);
    var $re = new RegExp($match,"g");
    $html = $html.replace($re, $newFormat);
  }
  return $(this).html($html);
}
$.fn.reformatallates=函数(){
var$结果,
$reg=newregexp(/\d{1,2}\/\d{1,2}\/\d{4}/),
$r2=新的RegExp(/[\n\r\f]/g),
$html=$(this.html();
$html=$html.replace($r2',);
$html=$html.replace($r2',);
var reformatDate=函数($str){
变量$parts=$str.split('/'),
$year=$parts[2],
$month=$parts[0],
$day=$parts[1];
如果($parts.length==3){
$month=$month.length<2?'0'+$month:$month;
$day=$day.length<2?'0'+$day:$day;
返回$year+'-'+$month+'-'+$day;
}
返回$str;
};
var$match,$newFormat,$msg;
while(($result=$reg.exec($html))!==null){
$match=$reg.exec($html)[0];
$newFormat=重新格式化日期($match);
var$re=新的RegExp($match,“g”);
$html=$html.replace($re,$newFormat);
}
返回$(this.html($html);
}

以下是我的想法。要了解我是如何得出以下结论的,请通读问题下的评论

jQuery(document).ready(function($) {
    var reg = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);

    $.fn.reformatAllDates = function (subselect) {
        var $this = $(this),
            $nodes,
            $node,
            text,
            matched;

        if (subselect) {
            $nodes = $this.find(subselect);
        } else if ($this.is('table')) {
            $nodes = $this.find('td');
        } else if ($this.is('dl')) {
            $nodes = $this.find('dt, dd');
        } else {
            $nodes = $this.children();
        }

        for (var i = 0, l = $nodes.size(); i < l; i++) {
            $node = $($nodes[i]);
            text = $node.text();
            matched = text.match(/\//g);

            if (matched !== null && matched.length == 2) {
                $node.reformatDate(text);
            }
        }
    };

    $.fn.reformatDate = function(text, parts) {
        var $this = $(this),
            matched,
            year,
            month,
            day;

        if (!parts) {
            text = text ? text : $this.text();
            matched = reg.exec(text);
            parts = matched !== null ? matched[0].split('/') : [];
        }

        if (parts.length === 3) {
            month = parts[0];
            day = parts[1];
            year = parts[2];

            month = month.length < 2 ? '0' + month : month;
            day = day.length < 2 ? '0' + day : day;

            //Returns dates in sort friendly format [YYYY-MM-DD]
            $this.html(year + '-' + month + '-' + day);
        }
    };

    $('#trigger').click(function(){
        $('#tabledata').reformatAllDates();
    });
});
jQuery(文档).ready(函数($){
var reg=newregexp(/^\d{1,2}\/\d{1,2}\/\d{4}$/);
$.fn.reformatallates=函数(子选择){
变量$this=$(this),
$nodes,
$node,
文本,
匹配;
如果(子选择){
$nodes=$this.find(子选择);
}else if($this.is('table')){
$nodes=$this.find('td');
}else if($this.is('dl')){
$nodes=$this.find('dt,dd');
}否则{
$nodes=$this.children();
}
对于(var i=0,l=$nodes.size();i

您可以将
td
选择和循环移动到插件中,而无需更改访问数据的方法,从而为您提供“理想的实现”@Mathletics,我实际上是在尝试避免循环,例如,我已经在非表上使用了它,比如数据定义列表。我的逻辑是,如果有1000个日期,但只有25个唯一的日期,在innterHTML上执行正则表达式会更有意义。但是当我尝试:这一点时,myregex根本不匹配。我想你可能把
reformatallates()
放在了错误的元素上。请参阅:一些可能更容易处理的内容:对于所有使用
$.html()
而不是
$.text()
来设置重新格式化日期的浏览器来说,时间似乎更好:在FF中重新格式化9秒,在Chrome中重新格式化0.43秒。看起来,
$this.html(格式化日期)
是目前的赢家。有问题的小提琴: