1

我有一个 HTML 表格,其中的单元格包含遵循特定模式的日期。当单元格包含日期时,同一列中的所有单元格都是相同模式的日期。如何使用 jQuery 匹配格式并返回列?这是一个演示模式的示例表:

+-------+-----------+-----------------------+-----------------------+----------+
| Type  | Compiled? |    Modified (CDT)     |     Created (CDT)     | Priority |
+-------+-----------+-----------------------+-----------------------+----------+
| Fancy | yes       | Oct 18, 2013 16:17:00 | Oct 08, 2013 05:50:32 |       75 |
| Fancy | yes       | Oct 18, 2013 16:16:28 | Oct 18, 2013 15:46:05 |       75 |
| Fancy | yes       | Oct 18, 2013 16:15:25 | Oct 17, 2013 19:04:51 |       75 |
+-------+-----------+-----------------------+-----------------------+----------+
4

1 回答 1

1

您可以尝试以下方法:

$('table td').filter(function () { return /.../.test($(this).html()); }).map(function () { return $(this).index(); } );

...其中 /.../ 是与您要查找的日期格式匹配的正则表达式。这将返回一个列索引数组,其中包含该模式的至少一个匹配项。这不是一个唯一的列表。如果您只是在寻找一种模式,以下正则表达式将起作用,而不是绝对需要确保它是一个有效的日期......

/^\w{3}\s\d+,\s\d{4}\s\d{1,2}:\d{2}:\d{2}$/

(这将与 Ziptember 45 日的 99 点相匹配,因此可能会获得更好的正则表达式。)

我将把那条密集的线分解一下,以更好地说明事情:

date_filter = function () { return /^\w{3}\s\d+,\s\d{4}\s\d{1,2}:\d{2}:\d{2}$/.test($(this).html())}

map_td_to_index = function () { return $(this).index(); }

console.log($('table td').filter(date_filter).map(map_td_to_index));

编辑:删除了 $.unique 的错误用法,它仅适用于 DOM 元素数组。你可以使用这样的东西:

$.fn.extend({ unique: function() 
              {
                arr = $.makeArray(this);
                return $.grep(arr, function(elem, idx) 
                                   {
                                     return $.inArray(elem, arr, idx + 1) === -1;
                                   }); 
              } 
            });

然后.unique()在调用 to 之后调用map()

于 2013-10-20T19:36:07.653 回答