1

HTML 字符串是:

"<div>\r\n<video controls=\"controls\" height=\"313\" id=\"video201643154436\" poster=\"/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg\" width=\"500\"><source src=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\" type=\"video/mp4\" />Your browser doesn&#39;t support video.<br />\r\nPlease download the file: <a href=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\">video/mp4</a></video>\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n<video controls=\"controls\" height=\"300\" id=\"video201644152011\" poster=\"\" width=\"400\"><source src=\"/uploads/ckeditor/attachments/24/test.mp4\" type=\"video/mp4\" />Your browser doesn&#39;t support video.<br />\r\nPlease download the file: <a href=\"/uploads/ckeditor/attachments/24/test.mp4\">video/mp4</a></video>\r\n</div>\r\n\r\n<p>&nbsp;</p>\r\n</div>\r\n"

我想将所有视频标签(包括其内容和子标签)替换为[[ Video ]]

预期的输出是:

"<div>\r\n[[ Video ]]\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n[[ Video ]]\r\n</div>\r\n\r\n<p>&nbsp;</p>\r\n</div>\r\n"

我曾尝试使用 regex /<video\s(.*?)<\/video(?=[>])>/,但它无法正常工作。

4

3 回答 3

1

我认为您需要替换这两个确切的字符串,以及此标签中的内容

首先是开始和结束字符串:

"<video "

"</video>"

puts html_text.gsub("<video ","[[ video ]] ").gsub('</video>',"[[ video ]]")

这应该工作

irb(main):020:0> <div>
[[ video ]]  controls="controls" height="313" id="video201643154436" poster="/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg" width="500"><source src="/uploads/ckeditor/attachments/23/newtons_law.mp4" type="video/mp4" />Your browser doesn&#39;t support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/23/newtons_law.mp4">video/mp4</a>[[ video ]]
</div>

<div>test description</div>

<div>
<div>
[[ video ]]  controls="controls" height="300" id="video201644152011" poster="" width="400"><source src="/uploads/ckeditor/attachments/24/test.mp4" type="video/mp4" />Your browser doesn&#39;t support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/24/test.mp4">video/mp4</a>[[ video ]]
</div>

<p>&nbsp;</p>
</div>
=> true

或使用正则表达式

puts html_text.gsub(/<\/?video[\s>]/, "[[ video ]]")

<div>
[[ video ]]controls="controls" height="313" id="video201643154436" poster="/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg" width="500"><source src="/uploads/ckeditor/attachments/23/newtons_law.mp4" type="video/mp4" />Your browser doesn&#39;t support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/23/newtons_law.mp4">video/mp4</a>[[ video ]]
</div>

<div>test description</div>

<div>
<div>
[[ video ]]controls="controls" height="300" id="video201644152011" poster="" width="400"><source src="/uploads/ckeditor/attachments/24/test.mp4" type="video/mp4" />Your browser doesn&#39;t support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/24/test.mp4">video/mp4</a>[[ video ]]
</div>

<p>&nbsp;</p>
</div>

最后把这个标签里面的所有东西都去掉,把所有的内容全部替换掉​​。问题是 \n 字符使用此修饰符:

/.*/m         multiline: . matches newline
/.*/i         ignore case
/.*/x         extended: ignore whitespace in pattern

所以最后如果我们一起加入正则表达式是:

puts html_text.gsub(/<video\s.*?<\/video>/mix, "[[ video ]]")

结果

irb(main):043:0> <div>
[[ video ]]
</div>

<div>test description</div>

<div>
<div>
[[ video ]]
</div>

<p>&nbsp;</p>
</div>
=> true
于 2016-05-04T10:22:31.170 回答
0

用正则表达式解析 html 是一项非常艰巨的任务。我建议使用nokogiri或类似的 gem 将其解析为 ast 并替换您需要的节点。

于 2016-05-04T09:59:32.170 回答
0

anquegi 的解决方案非常有效。与此同时,我尝试了 nokogiri:

str = "<div>\r\n<video controls=\"controls\" height=\"313\" id=\"video201643154436\" poster=\"/uploads/ckeditor/pictures/18/content_56883622_18f242e114.jpg\" width=\"500\"><source src=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\" type=\"video/mp4\" />Your browser doesn&#39;t support video.<br />\r\nPlease download the file: <a href=\"/uploads/ckeditor/attachments/23/newtons_law.mp4\">video/mp4</a></video>\r\n</div>\r\n\r\n<div>test description</div>\r\n\r\n<div>\r\n<div>\r\n<video controls=\"controls\" height=\"300\" id=\"video201644152011\" poster=\"\" width=\"400\"><source src=\"/uploads/ckeditor/attachments/24/test.mp4\" type=\"video/mp4\" />Your browser doesn&#39;t support video.<br />\r\nPlease download the file: <a href=\"/uploads/ckeditor/attachments/24/test.mp4\">video/mp4</a></video>\r\n</div>\r\n\r\n<p>&nbsp;</p>\r\n</div>\r\n"

doc =  Nokogiri::HTML(str)

doc.css("video").each do |video|
  new_node = doc.create_element "p"
  new_node.inner_html = "[[ Video ]]"
  video.replace new_node
end

new_str = doc.css("body").to_s
于 2016-05-04T12:36:59.897 回答