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"
我尝试使用正则表达式/<video\s(.*?)<\/video(?=[>])>/
,但它无法正常工作。
答案 0 :(得分: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'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 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> </p>
</div>
=> true
答案 1 :(得分:0)
使用正则表达式解析html非常困难的任务。我建议使用nokogiri
或类似的gem来解析它并替换你需要的节点。
答案 2 :(得分: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'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_s