473,394 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

variables only after second call?

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:

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

etc.

And here even stranger:

jsa="444"
444
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
1
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
445
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
2
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
446
jsa=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
Apr 8 '07 #1
1 1230
es_ wrote on 08 apr 2007 in comp.lang.javascript:
And here even stranger:

jsa="444"
444
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
1
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
445
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
2
jsa=a.replace(/(\d+)/, Number(RegExp.$1)+1)
446
jsa=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)
Apr 8 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Pack Fan | last post by:
I've noticed that session variables will persist on Mac IE even after all browser windows have been closed. One must quit the program to clear the session variables. This presents a security risk...
7
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
7
by: David | last post by:
i think i just realized i'm an idiot. again. (not syntactically correct code... just pieces to illustrate) class StateObject { members like socket, receiveBuffer, receiveBufferSize,...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.