Solved

Regex search in javascript

Posted on 2013-10-27
8
216 Views
Last Modified: 2013-10-30
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
Comment
Question by:sniger
  • 3
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:Sar1973
ID: 39605002
Try to look at the global HTML attributes supported by the label tag (http://www.w3schools.com/tags/tag_label.asp).
0
 
LVL 9

Accepted Solution

by:
Derek Jensen earned 500 total points
ID: 39606572
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
 

Author Comment

by:sniger
ID: 39609959
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Expert Comment

by:Sar1973
ID: 39611099
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
 

Author Comment

by:sniger
ID: 39612052
Sar1973, pelase understand, that everything is in the buffer (string), it is not DOM
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39612431
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
 

Author Comment

by:sniger
ID: 39612462
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
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39612472
Cool, thanks. Glad I could help! :-)
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question