variables only after second call?

Discussion in 'Javascript' started by es_, Apr 8, 2007.

  1. es_

    es_ Guest

    Hi,

    On other javascript group I didn't get answer. Maybe You'll know...

    There's a strange problem with variables. It feels that they work only
    after second call. It looks like this:

    js> a="1"
    1
    js> a=a.replace(/(\d)/, Number(RegExp.$1)+1)
    1
    js> a=a.replace(/(\d)/, Number(RegExp.$1)+1)
    2
    js> a=a.replace(/(\d)/, Number(RegExp.$1)+1)
    2

    etc.

    And here even stranger:

    js> a="444"
    444
    js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    1
    js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    445
    js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    2
    js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    446
    js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    3

    etc.

    Do anyone know why it is like this? And how to eliminate this effect?

    greetz,
    T
     
    es_, Apr 8, 2007
    #1
    1. Advertising

  2. es_

    Evertjan. Guest

    es_ wrote on 08 apr 2007 in comp.lang.javascript:

    > And here even stranger:
    >
    >js> a="444"
    > 444
    >js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    > 1
    >js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    > 445
    >js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    > 2
    >js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    > 446
    >js> a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    > 3
    >


    Not so strange,
    as the Regexp object is only filled AFTER the replace has completed.

    See:

    <script type='text/javascript'>

    a="444"
    document.write('a = '+a+'<br>') //444
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //empty

    a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    document.write('a = '+a+'<br>') //1
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //444

    a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    document.write('a = '+a+'<br>') //445
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //1

    a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    document.write('a = '+a+'<br>') //2
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //445

    a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    document.write('a = '+a+'<br>') //446
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //2

    a=a.replace(/(\d+)/, Number(RegExp.$1)+1)
    document.write('a = '+a+'<br>') //3
    document.write('RegExp.$1 = '+RegExp.$1+'<br><br>') //446

    </script>

    > Do anyone know why it is like this?


    Yes, anyone does

    > And how to eliminate this effect?


    Yes, do not use the Regexp object in a replace().

    [The global Regexp object is ment to be used
    AFTER the completion of the match function.]

    <script type='text/javascript'>

    a = "abc 444 abc"
    document.write(a+'<br>') //abc 444 abc

    a = a.replace(/(\d+)/, function(x,z){return +z +1})
    document.write(a+'<br>') //abc 445 abc

    a = a.replace(/(\d+)/, function(x,z){return +z +1})
    document.write(a+'<br>') //abc 446 abc

    a = a.replace(/(\d+)/, function(x,z){return +z +1})
    document.write(a+'<br>') //abc 447 abc

    a = a.replace(/(\d+)/, function(x,z){return +z +1})
    document.write(a+'<br>') //abc 448 abc

    </script>

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Apr 8, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    1
    Views:
    439
    S. Justin Gengo
    Aug 3, 2003
  2. Dirk Meusel
    Replies:
    1
    Views:
    3,074
    Dirk Meusel
    Aug 19, 2003
  3. panik
    Replies:
    0
    Views:
    461
    panik
    Dec 4, 2003
  4. Raymond O'Connor
    Replies:
    1
    Views:
    174
    Raymond O'Connor
    Mar 8, 2007
  5. yelipolok
    Replies:
    4
    Views:
    292
    John W. Krahn
    Jan 27, 2010
Loading...

Share This Page