在javascript中从url提取日期

Extract date from url in javascript

本文关键字:提取 取日期 url 中从 javascript      更新时间:2023-09-26

这是我的锚标签

<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>