Solved

Regex search in javascript

Posted on 2013-10-27
8
208 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

by:Sar1973
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

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

Expert Comment

by:Derek Jensen
Comment Utility
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
Comment Utility
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
Comment Utility
Cool, thanks. Glad I could help! :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now