2

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...

4
  • 1
    (Come to think of it, this is better as a comment) What's wrong with this regex? var str = "12/31/2011 12:00:00 AM after: 12/31/2011"; str = str.match(/[^\s]+/)[0]; console.log(str); Commented Oct 13, 2011 at 23:16
  • Is there a compelling reason to use a regex for this? Seems like the date would stop at the first space. Commented Oct 13, 2011 at 23:57
  • Hi Dave...working on an MVC3 app...returning a "SelectList" from a controller action assigned to a ViewBag variable, SalesWeekId. Displaying through an Html helper...need a short date to display. The Data Annotation from the Model isn't taking affect used this way, not sure why...using the annotations without issue throughout the application...searched a bit...didn't find anything...seemed ok for now. Commented Oct 14, 2011 at 0:32
  • @djs I'm just saying that a regex seems like overkill since you know the format you're getting, and have a consistent separator. Commented Oct 14, 2011 at 0:52

1 Answer 1

7

You aren't assigning the result.

item = item.replace(regex,'');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.