-1

I am looking for a method to remove a varying set of numbers from a string.

I have multiple images that have a style="" tag that I want to remove when rendering for an API.

i.e.

< img src="https://IMAGE.jpg" style="width: 1398px;" data-status="ok">

< img src="https://IMAGE.jpg" style="width: 1393px;" data-status="ok">

< img src="https://IMAGE.jpg" style="width: 1385px;" data-status="ok">

I want to remove the style="" tag and I am looking for a gsub or regex method to do this.

Something along the lines.

.gsub('style="width: ****px;"', '')

Where the **** is a solution to any number 0..9 for each digit?

Alexander Smith
  • 157
  • 2
  • 12
  • 1
    Please read through the formatting help that's available in the text entry box. Formatting correctly, for readability and to match the common style, helps the site and helps us help you. Please read "[ask]" and the linked pages, along with "[mcve]" and http://meta.stackoverflow.com/q/261592/128421. We'd like to see what you tried, rather than what your requirements for code are. – the Tin Man Dec 07 '16 at 21:11

1 Answers1

2

Although you probably don't want to do this with a regular expression, the hack way of doing this is:

gsub(/style="width:\s*\d+px;?"\s*/, '')

Here the key is \d+ which means one-or-more-digits.

The better way is to use an HTML parser like Nokogiri to remove the attribute:

doc = Nokogiri::HTML(source)
doc.css('img').each do |img|
  img.remove_attr('style')
end
doc.to_s

There's also ways of stripping all style tags.

Community
  • 1
  • 1
tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    Nokogiri, or a similar parser, is definitely the way to go. Using a pattern to manipulate HTML is very fragile, especially if the HTML generation is owned by someone else. – the Tin Man Dec 07 '16 at 21:16
  • @theTinMan Absolutely. I just put this out there as a work-around in case someone's battling Nokogiri which can be, at times, a giant mess to install. When it works, though, it works very well. – tadman Dec 07 '16 at 21:25
  • Every time I ran into problems with a Nokogiri installation it was due to the libXML stuff on my host. Once I got that straightened out Nokogiri was easily installed. That installation difficulty isn't limited to Nokogiri, a lot of tools that rely on drivers and shared libraries can be a major pain that way. :-/ – the Tin Man Dec 07 '16 at 22:03