Q:

Extract only years from strings in different formats in javascript

I have data from a public API and I need to extract years from a column in that dataset.
The text from that column has different formats, so let's say it's an array like this: 
var array = ['ca 1940', '4 October 1935', '1915', 'Undated', 'ca 1921', '5 September 1925']

How can I filter this array to have an array like this:
var years = magical_method(array); 
// years = ['1940', '1935', '1915', '1921', '1925']
// Remove strings which do not have years and extract years from strings which do have years

Any help will be appreciated.
Updated: I have tried:
function removeUndated(year) {
    if (year) {
      return year.match(/[\d]{1}/); 
    }
}

to remove the 'Undated' string, but I still don't have any ideas how to extract the years from strings with different formats as above.

A:

It seems that the year value will be the last word of the string so you can split that with respect to whitespace and then get the last word to check if it is a integer or not to satisfy that it is actually a year value.

var array = ['ca 1940', '4 October 1935', '1915', 'Undated', 'ca 1921', '5 September 1925'];
var res = [];
array.forEach((item) => {
  var year = item.split(/\s+/).pop();
  if(parseInt(year)){
    res.push(year);
  }
});
console.log(res);

