在文本节点中获取字母索引?

时间:2013-12-27 07:57:19

标签: css ruby nokogiri

<span class="word">fab
  <span class="green">u</span>lous
</span>

这段HTML只是一个“神话般的”字,带有绿色字母“u”。我需要找出这个单词中绿色字母的索引。

CSS .word选择整个单词,.green可以告诉字母'u'是绿色的。但实际上我不知道哪个'你'是绿色的。

如何获得绿色字母位置?

1 个答案:

答案 0 :(得分:3)

我想你想在一个单词中找到绿色字母的位置,所以我可以这样做:

require 'nokogiri'

str1 = '<span class="word">fab<span class="green">u</span>lous</span>'
str2 = '<span class="word">fabulo<span class="green">u</span>s</span>'

def get_green str
  doc = Nokogiri::HTML(str)
  sh = 0
  doc.css('.word').children.each do |c|
    sh += c.text.length
    if c['class'] == 'green'
      break
    end
  end
  sh - 1
end

p get_green(str1)
p get_green(str2)

输出结果为:

# => 3
# => 6

绿色字母(如果有)的出现和位置:

require 'nokogiri'

str1 = '<span class="word">fab<span class="green">u</span>lous</span>'
str2 = '<span class="word">fabulo<span class="green">u</span>s</span>'
str3 = '<span class="word">fabulo<span class="red">u</span>s</span>'

def get_green str
  doc = Nokogiri::HTML(str)
  char = doc.css('.word .green').text
  return [-1, -1] if char == ""
  occurance = 0
  position = 0
  doc.css('.word').children.each do |el|
    break if el['class'] == 'green'
    position += el.text.length
    occurance += el.text.chars.select{|ch| ch == char}.length
  end
  [occurance + 1, position]
end

p get_green(str1)
p get_green(str2)
p get_green(str3)

输出:

# => [1, 3]
# => [2, 6]
# => [-1, -1]