?
Solved

Regex search in javascript

Posted on 2013-10-27
8
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

801 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