0

Can somebody please help me construct a regular expression in Javascript to check if year 2017 exists or a year starting with 19 or 20 (century) exists in a given date.

My date format is : Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time).

I tried this but it fails:

var str = "Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)";
var patt = new RegExp("/^\S{3}[\s]\S{3}[\s]\d{2}[\s]\d{4}$/");
var res = patt.test(str);

Thanks, Haseena

dda
  • 6,030
  • 2
  • 25
  • 34
Haseena Parkar
  • 939
  • 2
  • 12
  • 28
  • 2
    Use `var patt = /^\S{3}[\s]\S{3}[\s]\d{2}[\s]\d{4}$/;` or escape backslashes in string when using `RegExp` constructor. – Tushar Dec 29 '16 at 05:34
  • 2
    Why not just use the Date('that long date string').getFullYear() – mattdevio Dec 29 '16 at 05:37
  • Possible duplicate of [Get the Current Year in JavaScript](http://stackoverflow.com/questions/6002254/get-the-current-year-in-javascript) – Gaurav Lad Dec 30 '16 at 06:59

4 Answers4

3

You can use Date.getFullYear() method.

var d = new Date("Fri Dec 01 1999 00:00:00 GMT-0800 (Pacific Standard Time)");
var year = d.getFullYear();
Gangaraju
  • 4,406
  • 9
  • 45
  • 77
  • It is easier in code to convert the string into a `Date` object to retrieve the year, but it's more work for the computer and completely unnecessary. – Vince Dec 29 '16 at 06:27
  • @Tamilselvan: i got year from above code and applied a regular expression on it ^(19|20) – Haseena Parkar Dec 30 '16 at 05:13
0

The year validation with multiple condition is not quite a regex thingy

If I have understood your question then you can try this:

^[a-zA-Z]{3}\s+[A-Za-z]{3}\s+\d{2}\s+(2017|(?:19\d{2}|20\d{2}))\s+.*
  1. It will only match if the year is 2017
  2. It will match if the year starts with 19
  3. It will match if the year starts with 20
  4. If match is found, then the year can be found in group 1

Explanation

const regex = /^[a-zA-Z]{3}\s+[A-Za-z]{3}\s+\d{2}\s+(2017|(?:19\d{2}|20\d{2}))\s+.*$/gm;
const str = `Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)`;
let m;

 if((m = regex.exec(str)) !== null) {
   console.log("Full match ="+m[0]);
   console.log("Matched year ="+m[1]);
}
else
  console.log("no match found");
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
  • This is way too complex. Why use character ranges and whitespace metacharacters when only the year is required? Why not just extract the year with `.match` and test the value using `String` methods? Why specify a non-capturing group when a capturing group would allow you to retrieve the full year regardless of what it is? Why use the multiline flag? – Vince Dec 29 '16 at 06:26
  • Why would you just pick a 4 digit without knowing if the rest of the string really is in date format ? There can be 1000s of solution for the op's problem ... but as his desire is to fix it only via regex therefore my solution is to comply with his requirments – Mustofa Rizwan Dec 29 '16 at 07:10
  • That's his criteria... he has a string representing a date in a fixed format that doesn't vary and he wants to make a decision based on the year in that string. – Vince Dec 29 '16 at 07:38
0

Here is example you can use.

var str="Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)";
var patt=/(\w{3}\s){2}\d{2}\s(\d{4})\s(.*)/;
var results = patt.exec(str);
console.log(results);
console.log(results[2]);

The second group is match the year.

Jeffy Zhang
  • 111
  • 2
  • 11
  • Too complex... Why do you explicitly match *word* characters, whitespace characters, and digits before or after the year? There are 11 characters that aren't relevant to the problem before the year, so you could just use `.` and it doesn't even matter what's after the year. – Vince Dec 29 '16 at 06:35
  • 1
    Yes, If only need the year it can simplify the Regular Expression. Thank you for advice. – Jeffy Zhang Dec 29 '16 at 06:52
-1

With a fixed date format, this is really easy:

First, just extract your year using a regular expression var year = str.match(/^.{11}(\d{4})/)[1]

.match returns an array where the first element is the entire matched string and subsequent elements are the parts captured in parentheses.

After you have this, you can test if year === '2017' or if year.substr(0, 2) === '19'.

There are about a dozen other ways to do this, too.

var myDate = 'Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)';

function isCorrectYear(dateString) {
  var year = dateString.match(/^.{11}(\d{4})/)[1];
  if (year === '2017') return true;
}

if (isCorrectYear(myDate)) {
  alert("It's the correct year.");
}

Something just occurred to me... "year 2017 exists or a year starting with 19 or 20". Doesn't that cover every year in every date string you are ever likely to encounter? I don't think they had Javascript before the 1900s and I doubt anything any of us will write will still be in use after the year 2100.

Vince
  • 3,962
  • 3
  • 33
  • 58