SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Regex replace

  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex replace

    Hi,
    The ugly thing I've done
    Code JavaScript:
    var str = 'Your confirmation was successful 5 seconds to redirect to login page';
    var newstr = str.replace(/(\d)/, "$1-1");

    What's the right way ?

    Bye

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code javascript:
    var str = 'Your confirmation was successful 5 seconds to redirect to login page';
    var newstr = str.replace(/(\d)/, function(s, p) {
      return p-1;
    });

    The English is pretty bad though. I'd change it to this: 'Your confirmation was successful. In 5 seconds you will be redirected to the login page.';

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Raffles View Post
    Code javascript:
    var str = 'Your confirmation was successful 5 seconds to redirect to login page';
    var newstr = str.replace(/(\d)/, function(s, p) {
      return p-1;
    });

    The English is pretty bad though. I'd change it to this: 'Your confirmation was successful. In 5 seconds you will be redirected to the login page.';
    Thanks for the code and for the english as well

  4. #4
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I managed this code
    Code JavaScript:
    function countDownRedirect(sel){
      var sec,re = /(\d)/;
      var str = $(sel).text();
      var found = str.match(re);
      if(null === found){return;}
      sec = found[0];
      var intervalId = setInterval(f,1000);
      function f(){
        var newstr = str.replace(/(\d)/, function(s, p) {
          return p-1;
        });
        if(sec>0){
          str = newstr;
          sec--;
          $(sel).text(newstr);
        }
        else{
          clearInterval(intervalId)
        }
      }
    }

    Do you have any advice ?

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Advice for what? Does it work?


Bookmarks

Posting Permissions

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