Javascript regex to extract year from string

I'm trying to extract the year from a timestamp using regex, but I'm not very good at it, so my code isn't returning what expected.

that's the string: 'Sun Jan 01 2012 00:00:00 GMT+0000 (GMT)'

I need to extract '2012' here's my code:

var scrollDate = tl.getBand(0).getCenterVisibleDate(); // my timestamp

// regex function
function extractNumbers(str) {
     var m = /(^|\s)(\d{4})(\s|$)/.exec(str);
     return m;
}

// click event
$('.n-left').click(function(){
     alert(extractNumbers(scrollDate));
});

The alert box returns this: 2012 , ,2012,

What am I doing wrong? Thanks in advance

Answers


var x = new Date("Sun Jan 01 2012 00:00:00 GMT+0000 (GMT)");

 alert(x.getUTCFullYear());

Need Your Help

send fileupload to WCF

c# javascript asp.net wcf

I have fileupload and some input text,I want to use WCF to update values to DB.

PHP DOMDocument instantiation syntax error

php dom html-parsing

I have been pulling my hair above below snippet of code - which is syntatically correct.. However I keep getting the following error when I put it on the server:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.