Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts

    replace not working for some reason

    I am trying to fix some datetime strings so I can convert form values into date objects so I can compare the dates in my validation process. the date time strings look like - 7/21/2011 8:00am

    for some reason the am is not being replaced by " am" . here's the function

    Code:
    	function ampm(str){
    
    		str.replace(/am/gi," am")
    		str.replace(/pm/gi," pm")
    		return str
    	}
    for example the result of ampm("8:00am") is 8:00am and not 8:00 am

    .. it seems so simple.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    nevermind.

  • #3
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by DanInMa View Post
    nevermind.
    It's customary to publish your solution, in this case that in JS strings cannot be altered and must be recreated:
    Code:
    <script type="text/javascript">
    
    function ampm(str)
    {
      return str.replace( /(am|pm)/i, " $1" );
    }
    
    alert( ampm("8:00am") );
    
    </script>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,564 Times in 2,542 Posts
    Code:
    str = str.replace(/am/gi," am");
    str = str.replace(/pm/gi," pm");
    #All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    str = str.replace(/am/gi," am");
    str = str.replace(/pm/gi," pm");
    #All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    While we're at presenting helpful hints, here's one of mine:

    Usually, coders will write something like this:
    Code:
        text = text.replace(blah1);
        text = text.replace(blah2);
        text = text.replace(blah3);
    ....etc....
    It can be done this way also:
    Code:
        text = text
            .replace(blah1)
            .replace(blah2)
            .replace(blah3);
    It's easier to read than stringing them all together:

    text = text.replace(blah1).replace(blah2) .replace(blah3)

    Best of all, this hint is absolutely free!

    (and yes I know "blah" is not valid code...)
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •