DOM字符串解析器
我有一个 DOMstring 对象,它是我使用 XMLHttpRequest 从服务器获取的一些网页的文本。我需要从中剪切一个位于某些特定标签之间的子字符串。有什么简单的方法可以做到这一点吗? substring() 或 slice() 这样的方法在我的情况下不起作用,因为网页内容是动态的,所以我无法指定 substring 的开头和结尾(我只知道它被
和 )。
I have a DOMstring object, text of some web page which I get from server using XMLHttpRequest. I need to cut a substring from it, which lies between some specific tags. Is there any easy way to do this? Such methods as substring() or slice() won't work in my case, because content of the web page is dynamic, so I can't specify the beginning and the end of substring (I only know that it's surrounded by <tag>
and </tag>
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您知道周围标签的名称,这应该可行。
This should work, assuming you know the name of the surrounding tags.
在大多数(所有?)JavaScript 浏览器环境中,DOMString 只是作为字符串实现,因此您可以使用您喜欢的任何解析技术,包括正则表达式、DOMParser 以及 jQuery 等库提供的 HTML 解析器。例如:
当然,这是一个糟糕的主意;你真的应该使用 DOM 解析器,例如,使用 jQuery:
[Edit] 为了澄清上面的 jQuery 示例,它相当于执行如下操作:
此解决方案比正则表达式解决方案更好,因为它将处理正则表达式解决方案失败的任何 HTML 语法细微差别。当然,它可能需要一些跨浏览器测试,因此推荐像 jQuery(或 Prototype、ExtJS 等)这样的库。
A DOMString is just implemented as a string in most (all?) JavaScript browser environments so you can use any parsing technique you like, including regular expressions, DOMParser, and the HTML parser provided by libraries such as jQuery. For example:
Of course, this is a terrible idea; you should really use a DOM parser, for example, with jQuery:
[Edit] To clarify the above jQuery example, it's the equivalent of doing something like below:
This solution is better than a regex solution since it will handle any HTML syntax nuances on which the regex solution would fail. Of course, it likely needs some cross-browser testing, hence the recommendation to a library like jQuery (or Prototype, ExtJS, etc).
假设周围的标签在字符串中是唯一的...
或者
看起来应该可以解决问题
Assuming the surrounding tag is unique in the string...
or
Seems like it should do the trick
正如@Gus 但改进了,如果你只有文本并且标签被重复:
As @Gus but improved, if you only have text and the tags are repited: