从网址获取最后4个字符并与当前年份匹配

时间:2022-10-20 10:00:05
  1. Im trying to use Javascript to get the last 4 characters from my url that ends with something like this:

    我试图使用Javascript从我的网址获取最后4个字符,结尾如下:

    http://myURL/schedule-2016/
    http://myURL/schedule-2017/
    
  2. So I could use this conditional:

    所以我可以使用这个条件:

    var currentYear = new Date().getFullYear();
    var myurl = window.location.pathname;
    var lastFour = myurl.substr(myurl.length - 4);
    if(currentYear === lastFour){
    Something happens;
    }
    else{
    Something else happens;
    }
    

What am I doing wrong?

我究竟做错了什么?

2 个解决方案

#1


0  

You need to convert it to the number before comparing and also need to avoid / at the end if it contains.

您需要在比较之前将其转换为数字,并且如果它包含,还需要避免/最后。

var currentYear = new Date().getFullYear();
var myurl = window.location.pathname;
// replace the `/` at end then get the last 4 character
// parse it to Number
var lastFour = Number(myurl.replace(/\/$/,'').slice(-4));
if(currentYear === lastFour){
    // Something happens;
}
else{
    // Something else happens;
}

var str1 = 'http://myURL/schedule-2016/',
  str2 = 'http://myURL/schedule-2017/';

function check(str) {
  var currentYear = new Date().getFullYear();
  var lastFour = Number(str.replace(/\/$/, '').slice(-4));
  if (currentYear === lastFour) {
    console.log('equal');
  } else {
    console.log('not equal');
  }
}

check(str1);
check(str2);

#2


0  

The other fellows have already pointed out the main issues(casting to number and trimming trailing slash /).
I just wanted to suggest the additional alternative solution using Number constructor and String.match function:

其他人已经指出了主要问题(转换为数字和修剪尾随斜线/)。我只想使用Number构造函数和String.match函数建议其他替代解决方案:

var currentYear = new Date().getFullYear(),
    lastFour = Number(location.pathname.match(/\d{4}(?=\/$)/));

if (currentYear === lastFour) {
   // Something happens;
} else {
   // Something else happens;
}

#1


0  

You need to convert it to the number before comparing and also need to avoid / at the end if it contains.

您需要在比较之前将其转换为数字,并且如果它包含,还需要避免/最后。

var currentYear = new Date().getFullYear();
var myurl = window.location.pathname;
// replace the `/` at end then get the last 4 character
// parse it to Number
var lastFour = Number(myurl.replace(/\/$/,'').slice(-4));
if(currentYear === lastFour){
    // Something happens;
}
else{
    // Something else happens;
}

var str1 = 'http://myURL/schedule-2016/',
  str2 = 'http://myURL/schedule-2017/';

function check(str) {
  var currentYear = new Date().getFullYear();
  var lastFour = Number(str.replace(/\/$/, '').slice(-4));
  if (currentYear === lastFour) {
    console.log('equal');
  } else {
    console.log('not equal');
  }
}

check(str1);
check(str2);

#2


0  

The other fellows have already pointed out the main issues(casting to number and trimming trailing slash /).
I just wanted to suggest the additional alternative solution using Number constructor and String.match function:

其他人已经指出了主要问题(转换为数字和修剪尾随斜线/)。我只想使用Number构造函数和String.match函数建议其他替代解决方案:

var currentYear = new Date().getFullYear(),
    lastFour = Number(location.pathname.match(/\d{4}(?=\/$)/));

if (currentYear === lastFour) {
   // Something happens;
} else {
   // Something else happens;
}