Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

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

0
sniger
Asked:
sniger
  • 3
  • 3
  • 2
1 Solution
 
Sar1973Commented:
Try to look at the global HTML attributes supported by the label tag (http://www.w3schools.com/tags/tag_label.asp).
0
 
Derek JensenCommented:
Are you wanting to find the entire label?
Are you searching in the code text or the output HTML?
It would be much easier to put it all on one line before doing the search for it, but if you don't have control over the original code, then try something like this:

/<label.*?(\r|\n)+.*?-\$val.*?label>/i

Open in new window

As demonstrated here. Of course replacing '-\$val' with the '-'+inp regexp.
That is, of course assuming it only spans two lines...and that you're searching the output HTML.

Need more info...
0
 
snigerAuthor Commented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Sar1973Commented:
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

0
 
snigerAuthor Commented:
Sar1973, pelase understand, that everything is in the buffer (string), it is not DOM
0
 
Derek JensenCommented:
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...
0
 
snigerAuthor Commented:
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
0
 
Derek JensenCommented:
Cool, thanks. Glad I could help! :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now