Javascript jQuery拆分文本

Javascript jQuery拆分文本,javascript,jquery,Javascript,Jquery,我有一个信用卡字段,用于填充标签,例如 <span>****-****-****-1111 (Expires 12/2012)</span> **-**-**-**-1111(2012年12月到期) 我需要提取日期,看看它是否在过去 目前,我有下面的jQuery,但我仍停留在split()的位置,无法仅提取日期 var $selectedDate = $('.prev-card .chzn-container .chzn-single span').text().sp

我有一个信用卡字段,用于填充
标签,例如

<span>****-****-****-1111 (Expires 12/2012)</span>
**-**-**-**-1111(2012年12月到期)
我需要提取日期,看看它是否在过去

目前,我有下面的jQuery,但我仍停留在split()的位置,无法仅提取日期

var $selectedDate = $('.prev-card .chzn-container .chzn-single span').text().split();
var $now = new Date();
if ($selectedDate < $now) {
    alert('past')
}
else{
    alert('future')
}
var$selectedDate=$('.prev card.chzn container.chzn single span').text().split();
var$now=新日期();
如果($selectedDate<$now){
警报(‘过去’)
}
否则{
警报(“未来”)
}
我认为这涵盖了所有内容,但请随意询问更多信息

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");
var selectedDate=$(“..”).text().match(/Expires(\d+)\/(\d+/),
expires=新日期(selectedDate[2],selectedDate[1]-1,1,0,0),
现在=新日期();
如果(expires.getTime()
试试这个:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");
var selectedDate=$(“..”).text().match(/Expires(\d+)\/(\d+/),
expires=新日期(selectedDate[2],selectedDate[1]-1,1,0,0),
现在=新日期();
如果(expires.getTime()
我不会拆分它。我会使用正则表达式:

var value = $('.prev-card .chzn-container .chzn-single span').text();
/\d+\/\d+/.exec(value)   //["12/2012"]

我不会分的。我会使用正则表达式:

var value = $('.prev-card .chzn-container .chzn-single span').text();
/\d+\/\d+/.exec(value)   //["12/2012"]

Kolink答案的小修正:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");
var selectedDate=$(“..”).text().match(/Expires(\d+)\/(\d+/),
expires=新日期(selectedDate[2],selectedDate[1]-1,1,0,0),
现在=新日期();
如果(expires.getTime()

(regexp不需要引号)

Kolink答案的小修正:

var selectedDate = $("...").text().match(/Expires (\d+)\/(\d+)/),
    expires = new Date(selectedDate[2],selectedDate[1]-1,1,0,0,0),
    now = new Date();
if( expires.getTime() < now.getTime()) alert("past");
else alert("future");
var selectedDate=$(“..”).text().match(/Expires(\d+)\/(\d+/),
expires=新日期(selectedDate[2],selectedDate[1]-1,1,0,0),
现在=新日期();
如果(expires.getTime()

(regexp不需要引号)

为什么要在客户端而不是服务器端执行此操作?@AnkitGautam可能是为了避免在客户端检测到琐碎的东西时往返服务器?我相信在服务器端也有类似的验证。这是什么。split()有什么用?没有什么用,这就是问题的关键所在,为什么你要用客户端而不是服务器端来做这件事?@AnkitGautam也许这是为了避免在客户端可以检测到一些琐碎的东西时往返服务器?我相信在服务器端也有类似的验证。这个.split()有什么用?没有什么用,这就是问题的关键所在