0

I am working with the following block of code:

<li class="standby" id="id4"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id5"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id6"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id7"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>

I have com across quite a few different patterns, but I can not seem to get any to work for what I need. I have been working with /^(?:<li>.*?</li>\s*)/ but I know it's way off. Basically I need to use regex to find and remove an LI based on the Id, which will be handled dynamically. so if the above were a menu, I would need to remove 5 for example. If I can get the regex working to highlight 5 for example in http://regexpal.com/ I should be able to wrap this up.

Update: I need to use a non js based function to accomplish this, so no jquery. specifically I am using the : http://cfquickdocs.com/cf9/#rematchnocase

Chris Hough
  • 3,389
  • 3
  • 41
  • 80

7 Answers7

1

Not sure about coldfusion specific syntax, but if it supports non-greedy quantifiers then this will work.

/<li[^>]*id="id5"[\s\S]*?<\/li>/i
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • disregard the last comment, I am going to test this now to see what if it works – Chris Hough Feb 23 '12 at 21:39
  • If you have to have it in regexpal then drop the regex literal syntax and select the case insensitive option. – Paul Alexander Feb 23 '12 at 21:39
  • yeah, tried your example in CF, so far no dice. how would I modify it for regxpal? I think that is a little closer to what cf accepts. – Chris Hough Feb 23 '12 at 21:49
  • here is a good example I just found http://www.bennadel.com/resources/demo/regular_expression_replace/index.cfm but this solution still barks – Chris Hough Feb 23 '12 at 21:56
1

The real answer here is that you should not be using a regular expression to parse HTML, because HTML is not a regular language.

Instead, use the Java library JSOUP from within your CF code to manipulate the HTML you're working with.

After you download the jar and add it to your CF classpath, you can do things like this:

<cfset jsoup = CreateObject("java", "org.jsoup.Jsoup")>
<cfsavecontent variable="html">
<li class="standby" id="id4"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id5"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id6"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
<li class="standby" id="id7"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI>
</cfsavecontent>

<cfset htmlObj = jsoup.parse(html)>
<cfset htmlObj.select('##id7').remove()>

<cfoutput>#htmlObj.html()#</cfoutput>

I've tested this, and it outputs exactly what you're asking for - the original HTML without the specified LI element.

Community
  • 1
  • 1
Jake Feasel
  • 16,785
  • 5
  • 53
  • 66
0

You've tagged your question with jQuery, so:

$("#id5").remove();

Will do it. Or if the id is in a variable:

var someId = "id5";
$("#" + someId).remove();

You don't need to worry about selecting by element type because you are assigning unique ids right?

Or to remove the _n_th li element:

$("li").eq(4).remove(); // remove fifth element (zero-based indexes)
nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

You appear to have tagged your question with way too many languages to make sense, but here's the answer for PHP:

<?php
$dom = new DOMDocument();
$dom->loadXML($data); // $data is your HTML stuff
// using loadXML so you don't get <html>, <head> and <body> tags added
$node = $dom->getElementById('5');
$node->parentNode->removeChild($node);
$result = $dom->saveXML();
?>
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I hear you, it's not perfect. thank you for your help. I tried your example @ regexpal.com, no luck. – Chris Hough Feb 23 '12 at 21:37