使用RegExp(Ruby)从HTML标记中提取字符串

Joh*_*ugh 1 ruby regex html-parsing

我想从字符串中提取"吐司" <h1>test</h1><div>toast</div>.什么正则表达式可以隔离这样的字符串?

编辑:感谢更正格式的用户.

更多信息:div标签总会只有一个实例,里面的信息可能会改变,但同一个字符串中永远不会有另一个div标签(字符串大于给定的样本)

谢谢!

Aru*_*hit 5

你可以用Nokogiri.

require 'nokogiri'

doc = Nokogiri::HTML::Document.parse("<div> test </div> <div> toast </div>")
doc.css('div').map(&:text)
# => [" test ", " toast "]
Run Code Online (Sandbox Code Playgroud)
require 'nokogiri'

doc = Nokogiri::HTML::Document.parse("<h1>test</h1><div>toast</div>")
doc.at_css('div').text
# => "toast"
Run Code Online (Sandbox Code Playgroud)