根据 LI ID 删除整个菜单 LI 项目
我正在使用以下代码块:
<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>
我已经遇到了很多不同的模式,但我似乎无法让任何模式满足我的需要。我一直在使用 /^(?:
但我知道它还很遥远。基本上我需要使用正则表达式根据 Id 查找和删除 LI,这将被动态处理。所以如果上面是一个菜单,我需要删除 5 个。如果我能让正则表达式突出显示 5 例如 http://regexpal.com/ 我应该能够总结一下。
更新: 我需要使用非基于 js 的函数来完成此操作,因此不需要 jquery。具体来说,我正在使用: http://cfquickdocs.com/cf9/#rematchnocase
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里真正的答案是你不应该使用正则表达式来解析HTML,因为HTML不是一种常规语言。
相反,请在 CF 代码中使用 Java 库 JSOUP 来操作您正在使用的 HTML。
下载 jar 并将其添加到 CF 类路径后,您可以执行以下操作:
我已经对此进行了测试,它输出的正是您所要求的内容 - 没有指定 LI 元素的原始 HTML。
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:
I've tested this, and it outputs exactly what you're asking for - the original HTML without the specified LI element.
不确定 Coldfusion 的特定语法,但如果它支持非贪婪量词,那么这将起作用。
Not sure about coldfusion specific syntax, but if it supports non-greedy quantifiers then this will work.
您已经用 jQuery 标记了您的问题,所以:
会这样做。或者,如果 id 位于变量中:
您无需担心按元素类型进行选择,因为您正在分配唯一的 id,对吧?
或者删除第 _n_th li 元素:
You've tagged your question with jQuery, so:
Will do it. Or if the id is in a variable:
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:
您似乎用太多语言标记了您的问题,没有意义,但这是 PHP 的答案:
You appear to have tagged your question with way too many languages to make sense, but here's the answer for PHP:
我认为Kolink提供的表达方式最适合你。假设您将 HTML 存储在名为“htmllist”的变量中,则此代码应该可以工作:
正如 Jake Feasel 提到的,HTML 不是常规的,但这应该可以工作。如果您的问题变得更加复杂(例如,如果您需要对不同属性的关联方式采取行动),基于 XML 的解决方案可能会很好地为您服务。
假设您的“htmllist”变量包含周围的“UL”标签并且是有效的 XML,则以下代码等效于上面发布的正则表达式解决方案。
这并不能更好地解决原始问题,但如果问题变得更加复杂,可能会有所帮助。
I think the expression provided by Kolink will serve you best. Assuming you have your HTML stored in a variable called "htmllist", this code should work:
As Jake Feasel mentioned, HTML is not regular, but this should work. If your problem gets more complicated (for example if you need to take action on how different attributes relate) an XML-based solution may serve you well.
Assuming that your "htmllist" variable includes the surrounding "UL" tags and is valid XML, the following code would be equivalent to the regular expression solution posted above.
This is no better to solve the original problem, but might help as a starting point if it gets more complicated.
我会这样做...它不使用 rematchnocase,而是使用 ColdFusion XML 实用程序、XMLSearch/XPath 查询,并返回预期结果。不过,您需要添加根元素 (ul)。
I would do it this way... It doesn't use rematchnocase, but rather uses ColdFusion XML utilities, an XMLSearch/XPath query, and returns the expected results. You'll need to add the root element though (ul).
感谢我的队友@Alex Brown 提供了更新的正则表达式函数,感谢@Paul Alexander 提供了有关 php/cf 版本的正确方向,感谢@Splash-x @nnnnn 提供了我需要的 jquery 版本。这是 cf 端的测试用例,一旦我获得 jquery 设置就会更新:
Thank you to my teammate @Alex Brown for the updated regex functions, @Paul Alexander for the correct direction on the php/cf versions and @Splash-x @nnnnn for the jquery versions I needed. here is a test case for the cf side, will update once I get the jquery setup: