在javascript中从url提取日期
这是我的锚标签在javascript中从url提取日期,javascript,html,Javascript,Html,这是我的锚标签 <a href="/availability-calendars/28/2011/12/edit?destination=node%2F28">edit</a> 我想使用javascript从href中提取“2011/12”。我不想使用拆分函数 有办法吗 var matches = "/availability-calendars/28/2011/12/edit?destination=node%2F28".match(/\/([1-2][09][0
<a href="/availability-calendars/28/2011/12/edit?destination=node%2F28">edit</a>
我想使用javascript从href中提取“2011/12”。我不想使用拆分函数
有办法吗
var matches = "/availability-calendars/28/2011/12/edit?destination=node%2F28".match(/\/([1-2][09][0-9]{2})\/([0-1]?[0-9])\//);
var year = parseFloat( matches[1] );
var month = parseFloat( matches[2] );
这应该适用于1900-2099年范围内的日期,它查找的是“/year/month/”
正则表达式使用:
/\/([1-2][09][0-9]{2})\/([0-1]?[0-9])\/
您可以使用此javascript代码
<a href="/write_pilot/availability-calendars/28/2011/12/edit?destination=node%2F28" id="ahref">Hello</a>
<script language="javascript" type="application/javascript">
window.onload = function()
{
str = document.getElementById('ahref').href;
str2 = "availability-calendar";
pos = str.indexOf(str2)+parseInt(str2.length) + 2;
date = str.substring(parseInt(pos),parseInt(pos)+10);
alert(date);
}
</script>
window.onload=函数()
{
str=document.getElementById('ahref').href;
str2=“可用性日历”;
pos=str.indexOf(str2)+parseInt(str2.length)+2;
日期=str.substring(parseInt(pos),parseInt(pos)+10);
警报(日期);
}
一定有办法。子字符串、切片等。但是拆分将是最简单的,那么为什么不告诉我们为什么不使用拆分?日期字符串前面是否总是有“可用性日历”,后面是“编辑”是的。它的前面总是“可用性日历”,后面总是“编辑”