2

I am trying to remove the "day" from every possible date occurrence on a page.

This is to make jQuery turn every date in the format of "08/22/2012" into "08/2012"

I was able to do this with this code: Replacing wildcard text using jquery

See my fiddle for more information: http://jsfiddle.net/CfZjF/223/

But it just isn't working within this table layout, regardless of what I have tried.

Another problem will be to specify the day specifically (maybe with wildcards?)-- that is the 2 numbers between the forward-slashes: /xx/, but please see the fiddle for more info.

Any ideas on how I can pull this off?

4

3 回答 3

2

Try

str.replace(/\/\d+\//g, "/");

Or be more specific by replacing /(\d{2})\/\d{2}\/(\d{4})/g with "$1/$2" or something…</p>

(Updated fiddle)

于 2012-08-22T17:37:28.223 回答
1

我认为您应该单独遍历表格单元格,而不是尝试对整行 HTML 进行全局处理。

这假设您的数据格式与您的 jsFiddle 中的一样。

更新小提琴:http: //jsfiddle.net/GKrCS/

$('tr').each(function(){
       $('td',this).not(':first').text(
           function(){
               return $(this).text().replace(/\/[0-9]+\//,'/');
           });
});
于 2012-08-22T17:47:16.440 回答
0

由于您所有的日期都采用 xx/xx/xxxx 的形式,因此使用简单的 split() 将始终将其拆分为具有以下值的数组:

xx,xx,xxxx

所以,像这样:

var totalDate = $("whateverYourDateSelectorMightBe");
var daysInMiddle = totalDate.val().ToString().split(",")[1];

那么你可以这样做:

totalDate.val(totalDate.val().ToString().replace(daysInMiddle + "/",""));

请注意,有很多更简洁的方法可以做到这一点。我这样做是因为我认为它更好地解释了我想要做的事情。

于 2012-08-22T17:48:07.350 回答