variables only after second call?

E

es_

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
 
E

Evertjan.

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

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top