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'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'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> </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> </p>\r\n</div>\r\n"我尝试过使用regex /<video\s(.*?)<\/video(?=[>])>/,但它没有正常工作。
发布于 2016-05-04 10:22:31
我认为您需要替换这两个确切的字符串,以及这个标记中的内容。
首先是开头和结尾的字符串:
"<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'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't support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/24/test.mp4">video/mp4</a>[[ video ]]
</div>
<p> </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'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't support video.<br />
Please download the file: <a href="/uploads/ckeditor/attachments/24/test.mp4">video/mp4</a>[[ video ]]
</div>
<p> </p>
</div>最后,要删除该标记和所有内容中的所有内容,请替换所有内容。问题是\n字符使用此修饰符:
/.*/m多行:。匹配换行符/.*/i忽略大小写/.*/x扩展:忽略模式中的空白
因此,最后,如果我们将所有的正则表达式连接在一起,那么正则表达式是:
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> </p>
</div>
=> true发布于 2016-05-04 09:59:32
用regex解析html非常困难的任务。我建议使用nokogiri或类似的gem将其解析为ast并替换所需的节点。
发布于 2016-05-04 12:36:59
anquegi的解决方案工作得很好。与此同时,我尝试了诺科吉里:
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'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'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> </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_shttps://stackoverflow.com/questions/37024324
复制相似问题