jQuery拆分文本

jQuery split text

本文关键字:文本 拆分 jQuery      更新时间:2024-03-15

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

<span>****-****-****-1111 (Expires 12/2012)</span>

我需要提取日期,看看它是否在过去。

目前,我有下面的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 = $("...").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 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");

(regexp不需要引号)