I have the following routine to replace the Time portion of a datetime string displayed in the options of a drop down list called "SalesWeek". The Replace method is not replacing the the portion of the string the regular expression is expected to match.
When the following routine runs in the browser (FF):
var select = $('#SalesWeek');
var item = '';
$.each($('#SalesWeek option'), function(){
item = $(this).text();
console.log('before: ' + item);
var regex = new RegExp(/(\s([0]\d|[1][0-2])(\:[0-5]\d){1,2})*\s*([aApP][mM]{0,2})/);
item.replace(regex,'');
console.log('after: ' + item);
$(this).html(item);
});
The following is printed to the console:
before: 12/31/2011 12:00:00 AM
after: 12/31/2011 12:00:00 AM
But I'm expecting:
before: 12/31/2011 12:00:00 AM
after: 12/31/2011
I've tried using $(this).html() and $(this).text() to get the initial value thinking perhaps there are characters I'm just not seeing in the console for either case...but it doesn't seem to make a difference.
I've tested the regular expression for use in javascript at the following link: http://www.regxlib.com/RETester.aspx
Using javascript engine
Test Expression: (\s([0]\d|[1][0-2])(\:[0-5]\d){1,2})*\s*([aApP][mM]{0,2})
Input Value: 12/3/2011 12:00:00 AM
Expected Match: 12:00:00 AM
It appears to work without issue. Notice that I stripped the leading "/" and trailing "/" on the expression when testing, but I believe the "/" is necessary when creating the new RegExp in javascript. Perhaps I'm wrong about that and there's another way, but I seem to get an error in firebug if the expression I'm trying to create is not enclosed that way.
I always seem to find answers I'm searching for on Stack Overflow to most issues I encounter...but I'm at a loss...so, I'm humbled enough to be the one asking the question this time...any help is very much appreciated...
var str = "12/31/2011 12:00:00 AM after: 12/31/2011"; str = str.match(/[^\s]+/)[0]; console.log(str);