0

I'd like to find headings and give them an id attribute. For example, I might want to add id="my-new-id" to <h2>or something like <h2 class="class-1 class-2">. The parser method looks like this:

def parse_toc(text)
  p = []
  text.split("\n").each do |line|
    if line.match(/\<h2.*?\>/)
      # need to add id
    end
    p.push line
  end
  return p.join("\n")
end

I have trouble keeping wildcards as part of the string. How can I keep a wildcard in a string while changing what surrounds it?

sawa
  • 165,429
  • 45
  • 277
  • 381
seancdavis
  • 2,739
  • 2
  • 26
  • 36

1 Answers1

1

Try following:

puts '<h2>'.gsub(/<h2/, '\& id="my-new-id"')
# <h2 id="my-new-id">
puts '<h2 class="class-1 class-2">'.gsub(/<h2/, '\& id="my-new-id"')
# <h2 id="my-new-id" class="class-1 class-2">

BTW, using regular expression to parse/modify html/xml is a bad idea. See this answer.

Community
  • 1
  • 1
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Great advice, @falsetru! I'm now using [Nokogiri](http://nokogiri.org/), and it makes parsing and modifying HTML super easy. – seancdavis Sep 15 '13 at 15:40