Search Support

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

Lolu chungechunge lwabekwa kunqolobane. Uyacelwa ubuze umbuzo omusha uma udinga usizo.

Major Problem with regExp.exec and regExp.test

  • 5 uphendule
  • 36 zinale nkinga
  • 21 views
  • Igcine ukuphendulwa ngu mradamjee

more options

I'm a programmer, and I'm using regExp's to simplify my life in a chat thing. Basically, ":blue:" in a chat will essentially be replaced with "". because there can be all sorts of colors, I can't use replace. Instead, I used regExp.exec(string). However, there is a major bug where every other time, the regExp.exec returns null even if there IS a match. If the chat is ":blue:", the response will alternate as :blue:,null,:blue:,null,etc. If the chat is ":blue: :red:" (e.g. two color blocks) the response will go :blue:,null,null,:blue:,null,null and so on. Basically, if there are 'x' color blocks defined, the .exec will work every x+1 times, and the other times it will not find a match. this also happens with regExp.test. I have gone through my code hundreds of times, and finally, I decided to run it in Opera. It works PERFECTLY fine. even in IE, it works. So i had to assume it was some bug with Firefox.

I hope this question catches the attention of the MOZILLA DEVELOPERS, as this is huge.

I'm not sure if it's just me. However, I have uploaded an html file onto http://jsbattle.tablocks.com/Untitled-1.html (the subdomain had a different purpose, but i figured i would put it here), and it will alert :blue: and null (at least for me). You can view the source code. An important note is that this .exec and .test glitch only happens inside functions. for example, regExp.exec twice just in a script tag will run fine, but while in a function abc() {regExp.exec} and executing abc(); abc(); it'll glitch out. I have not run extensive tests to see when this glitch occurs, this is all I know. Please help, Mozilla Developers. And if anyone knows how to contact the Mozilla Developers, tell me, or address them of this issue.

I'm a programmer, and I'm using regExp's to simplify my life in a chat thing. Basically, ":blue:" in a chat will essentially be replaced with "<span style='color:blue;'>". because there can be all sorts of colors, I can't use replace. Instead, I used regExp.exec(string). However, there is a major bug where every other time, the regExp.exec returns null even if there IS a match. If the chat is ":blue:", the response will alternate as :blue:,null,:blue:,null,etc. If the chat is ":blue: :red:" (e.g. two color blocks) the response will go :blue:,null,null,:blue:,null,null and so on. Basically, if there are 'x' color blocks defined, the .exec will work every x+1 times, and the other times it will not find a match. this also happens with regExp.test. I have gone through my code hundreds of times, and finally, I decided to run it in Opera. It works PERFECTLY fine. even in IE, it works. So i had to assume it was some bug with Firefox. I hope this question catches the attention of the MOZILLA DEVELOPERS, as this is huge. I'm not sure if it's just me. However, I have uploaded an html file onto http://jsbattle.tablocks.com/Untitled-1.html (the subdomain had a different purpose, but i figured i would put it here), and it will alert :blue: and null (at least for me). You can view the source code. An important note is that this .exec and .test glitch only happens inside functions. for example, regExp.exec twice just in a script tag will run fine, but while in a function abc() {regExp.exec} and executing abc(); abc(); it'll glitch out. I have not run extensive tests to see when this glitch occurs, this is all I know. Please help, Mozilla Developers. And if anyone knows how to contact the Mozilla Developers, tell me, or address them of this issue.

Isisombululo esikhethiwe

I think that it is about caching the RegExp because you specify the g flag.

Compare these:

function abc(R) {alert(R.exec(":blue:"));} abc(/\:blue\:/g);abc(/\:blue\:/g);

function abc(R) {alert(R.exec(":blue:"));} R=/\:blue\:/g; abc(R);abc(R);
function abc(R) {alert(R.exec(":blue:"));} R=/\:blue\:/; abc(R);abc(R);


See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec

Funda le mpendulo ngokuhambisana nalesi sihloko 👍 5

All Replies (5)

more options

Isisombululo Esikhethiwe

I think that it is about caching the RegExp because you specify the g flag.

Compare these:

function abc(R) {alert(R.exec(":blue:"));} abc(/\:blue\:/g);abc(/\:blue\:/g);

function abc(R) {alert(R.exec(":blue:"));} R=/\:blue\:/g; abc(R);abc(R);
function abc(R) {alert(R.exec(":blue:"));} R=/\:blue\:/; abc(R);abc(R);


See also https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec

Okulungisiwe ngu cor-el

more options

I found the same problem inside a FOR/EACH loop: The first time I get the regexp.exec result it's Ok, the second null, the next ok, the next null, etc

I found this only with FF.

But, when I alternate the var who receives the results it works fine:

   $('.fake' + id + ':input').each(
       function(index, element) {
           var m = /Save.+\([^\)].+\)/ig;
           var value = $('#' + element.id).attr('onclick').toString().replace('\n', ).replace('\r', ).replace('\t', );

           alert(value);
           var r = null;
           var r1 = null;
           
           r = m.exec(value);
           alert('a:' + r + '||' + value);
           r1 = m.exec(value);
           alert('b:' + r1 + '||' + value);
           if (r != null) {
               alert(id + ' ## \nr[0]:--\n' + r[0] + '--\nv:--\n' + value);
           }
           else {
               alert(id + ' -- \nis null\nv:--\n' + value);
           }
           r = null;
           
       }//-- /function
more options

Ok ... it's the 'g' flag but with IE don't occours

Okulungisiwe ngu Azznonimous

more options

Sorry for Spam, I'm searching for Bugs for Mozillas Bug Bounty Program!

I edited this post by myself ;)

Okulungisiwe ngu <script>alert(3133712)</script>

more options

I think this is a valid bug..even if we use different string, its the same issue.. consider this: function abc(R) {var exp = /\:blue(.*)\:/g; alert(exp.exec(R));} R=":blue2:"; abc(R);R=":blue3:";abc(R);

although the strings are different, we still see null match the second time.

Also as a feature, it doesn't make sense. The "g" flag is supposed to be used for global search and the expected natural behavior is that every time, I call exec, even with same string, it should search from begining. This is the behavior in Chrome and IE !

Thanks