Link to home
Start Free TrialLog in
Avatar of sniger
sniger

asked on

Regex search in javascript

I am finding the position of val by specifying html.search (new RegExp('-'+inp));

where inp = $val, but how to find the beginning of the string that is <label> ?

html = "
 <label class= 'w'> <input name = '$name' value = '$key' 
			type = 'checkbox' checked ='yes' > $key-$val  </label>";

<label class= 'w'> <input name = '$name' value = '$key' 
			type = 'checkbox' checked ='yes' > $key-$val  </label>"

..
...
 "

Open in new window

Avatar of Sar1973
Sar1973
Flag of Italy image

Try to look at the global HTML attributes supported by the label tag (http://www.w3schools.com/tags/tag_label.asp).
ASKER CERTIFIED SOLUTION
Avatar of Derek Jensen
Derek Jensen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sniger
sniger

ASKER

I want to find the index of the <label>  which is nearest to the string $val

lets say:
I have a string (which can be very long):
<label class="w"> <input name="innposn" id="inppos" class="w">   </label><label class="w"> <input name="Vend[]" value="376406" type="checkbox"> 376406-A.I. Editions Incorporated     </label><label class="w"> <input name="Vend[]" value="584116" type="checkbox"> 584116-A.J. Incorporated       

Open in new window


My inp would be: A.I. and I need the index of <label> which constains this string A.I.
I'm not sure you are looking for the ordinal number of the label which contains as value the string "A.I."; if so, try:
var myControl = document.getElementsByTagName("input");
var MyIndex=0;
for (var i=0; i<myControl.length; i++) {
	if (myControlI[i].getAttribute("type") == "label" && myControlI[i].indexOf("A.I.")>=0) {
	myIndex=i;
	} else {
	myControlI[i].checked=false;	
	}
}

Open in new window

Avatar of sniger

ASKER

Sar1973, pelase understand, that everything is in the buffer (string), it is not DOM
Well, this was as close as I was able to come to what you're asking for; I have no clue how to get it to work in...whatever it is that you're trying to do...
Avatar of sniger

ASKER

I made it work:

 <label class="w">\s+<input name="Vend\[]"+\svalue="\d{4,8}"\stype="checkbox">\s+.*-inp

Open in new window


You pointed me into the right direction. thanks
Cool, thanks. Glad I could help! :-)