解析器可以在HTML注释中读取,或者是Regex唯一的方法 [英] Can dom parsers read inside HTML comments, or is Regex the only way

查看:145
本文介绍了解析器可以在HTML注释中读取,或者是Regex唯一的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的组织创建了非常简单的CMS。



我的策略是在标签<可编辑>。但是,要将这些隐藏在浏览器中,我正在对它们进行评论。因此,可编辑区域的示例将如下所示。

 <! - < editable name =news_item>今天新闻是...< / editable>  - > 

随着内容今日新闻...被CMS接收,并在在线HTML编辑器中进行编辑。



我希望能够抓取name属性的值作为标签中包含的内容。

有没有一种简单的方法可以使用XPath,XQuey类型的东西,或者是正则表达式。考虑到正则表达式不需要太多的容错性,因为我确切知道xml是什么,因为我将编写生成它的代码)。

解决方案

通过DOM解析器,你的意思是JavaScript?如果是这样,这篇博文表明你确实可以切片和骰子HTML评论。而且,因为提到javascript而不提及jQuery是一种罪过,这里是一个可以为你找到所有HTML注释的jQuery插件。

I am creating very simple CMS for my organisation.

My strategy is to embed editable content between tags called < editable >. However to hide these from the browser I am commenting them out. So an example of an editable region will look like this.

<!-- <editable name="news_item> Today's news is ... </editable> -->

With the content "Today's news is ... " being picked up by the CMS and made editable in the online HTML editor.

I would like to be able to "grab" the name attribute's value as well as the content contained within the tags.

Is there a simple way to do this with XPath, XQuey type things, or is regex the best way to go ( ]esp. given that the regex will not need too much fault tolerance, since I know exactly what the xml will be, because I will be writing the code that generates it).

解决方案

By DOM Parser, do you mean javascript? If so, this blog post suggests that you can indeed slice and dice HTML comments. And, because mentioning javascript without mentioning jQuery is a sin, here's a jQuery plugin that will find all the HTML comments for you.

这篇关于解析器可以在HTML注释中读取,或者是Regex唯一的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆