CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Regular Expression headache (http://www.codingforums.com/showthread.php?t=206124)

bwalls 10-03-2010 10:08 PM

Regular Expression headache
 
I have a function that changes the label on a set of labels with similar "for" fields. Somehow, the matching is only matching every other label. I suspect I must be missing something obvious.

The goal is to change the text of a set of radio buttons to either "top" and "bottom" or "left" and "right" depending on orientation.

Code:

function ChangeLabels(orientation) {
        var lefttop =        "Left";
        var bottomright =        "Right";
    var lbls = document.getElementsByTagName("label");
    var lefttopregex = new RegExp ("Field6_[0-9]+", "gi");
    var bottomrightregex = new RegExp ("Field20_","gi");
   
   
    if (orientation == "Vertical") {
            lefttop = "Top";
            bottomright = "Bottom";
    }

  var forvalue = "";
  for (var i=0,k=lbls.length; i<k; i++) {
            forvalue = lbls[i].htmlFor;
   
          if (lefttopregex.test(forvalue)) {
                      lbls[i].innerHTML = lefttop;
    }
          if (bottomrightregex.test(forvalue)) {
                      lbls[i].innerHTML = bottomright;
    }
   
  }       
       
}

And here is the HTML of the labels.

Code:

<li id="fo1li6" class="    ">
<label class="desc" id="title6" for="Field6_0">
Left/Top
<span id="req_6" class="req">*</span>
</label>
<div>
<input id="radioDefault_6" name="Field6" type="hidden" value="" />
<span>
<input id="Field6_0" name="Field6" type="radio" class="field radio" value="Dogs" tabindex="20" onchange="handleInput(this);"  checked="checked" />
<label class="choice" for="Field6_0" >
Dogs</label>
</span>
<span>
<input id="Field6_1" name="Field6" type="radio" class="field radio" value="Pineapple" tabindex="21" onchange="handleInput(this);" />
<label class="choice" for="Field6_1" >
Pineapple</label>
</span>
<span>
<input id="Field6_2" name="Field6" type="radio" class="field radio" value="Leaves" tabindex="22" onchange="handleInput(this);" />
<label class="choice" for="Field6_2" >
Leaves</label>
</span>
<span>
<input id="Field6_3" name="Field6" type="radio" class="field radio" value="Cat" tabindex="23" onchange="handleInput(this);" />
<label class="choice" for="Field6_3" >
Cat</label>
</span>
<span>
<input id="Field6_4" name="Field6" type="radio" class="field radio" value="Star" tabindex="24" onchange="handleInput(this);" />
<label class="choice" for="Field6_4" >
Star</label>
</span>
<span>
<input id="Field6_5" name="Field6" type="radio" class="field radio" value="Bamboo &amp; Sun" tabindex="25" onchange="handleInput(this);" />
<label class="choice" for="Field6_5" >
Bamboo & Sun</label>
</span>
<span>
<input id="Field6_6" name="Field6" type="radio" class="field radio" value="Bamboo" tabindex="26" onchange="handleInput(this);" />
<label class="choice" for="Field6_6" >
Bamboo</label>
</span>
<span>
<input id="Field6_7" name="Field6" type="radio" class="field radio" value="Sun &amp; Moon" tabindex="27" onchange="handleInput(this);" />
<label class="choice" for="Field6_7" >
Sun & Moon</label>
</span>
<span>
<input id="Field6_8" name="Field6" type="radio" class="field radio" value="Fleur de lis" tabindex="28" onchange="handleInput(this);" />
<label class="choice" for="Field6_8" >
Fleur de lis</label>
</span>
<span>
<input id="Field6_9" name="Field6" type="radio" class="field radio" value="Dragonflies" tabindex="29" onchange="handleInput(this);" />
<label class="choice" for="Field6_9" >
Dragonflies</label>
</span>
<span>
<input id="Field6_10" name="Field6" type="radio" class="field radio" value="No Design" tabindex="30" onchange="handleInput(this);" />
<label class="choice" for="Field6_10" >
No Design</label>
</span>
</div>
</li>


Logic Ali 10-03-2010 11:39 PM

Quote:

Originally Posted by bwalls (Post 1000269)
I have a function that changes the label on a set of labels with similar "for" fields. Somehow, the matching is only matching every other label.

I don't think you need the g flag here and its use increments the lastIndex property of the regex.

Old Pedant 10-03-2010 11:53 PM

Remove the "g" from "gi". Then it works.

No, please don't ask me why.

Clearly, there never was a reason for the "g". But as to why it causes the failure... My head hurts.

Old Pedant 10-03-2010 11:54 PM

Quote:

Originally Posted by Logic Ali (Post 1000282)
I don't think you need the g flag here and its use increments the lastIndex property of the regex.

Teach me to not hit refresh.

Okay, I see it bumps lastIndex. But why does that matter, if you are doing a fresh call to test()???

Logic Ali 10-04-2010 12:43 AM

Quote:

Originally Posted by Old Pedant (Post 1000286)

Okay, I see it bumps lastIndex. But why does that matter, if you are doing a fresh call to test()???

Because it's still pointing to the end of the last match, so on the next call it doesn't search from the start of the string.


All times are GMT +1. The time now is 10:04 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.