Modifying text inside html nodes - nokogiri

前端 未结 2 1053
自闭症患者
自闭症患者 2021-02-15 14:14

Let\'s say i have the following HTML:

  • Bullet 1.
  • Bullet 2.
  • Bullet 3.
  • Bullet 4.&
2条回答
  •  既然无缘
    2021-02-15 14:38

    What about this code?

    doc.traverse do |x|
      if x.text?
        x.content = x.content.gsub(/(?<=[.!?])(?!\*)/, "#{$1}*")
      end
    end
    

    The traverse method does pretty much the same as search("*").each. Then you check that the node is a Nokogiri::XML::Text and, if so, change the content as you wished.

提交回复
热议问题