views:

79

answers:

4

So I found myself needing to remove <br /> tags from the beginning and end of strings in a project I'm working on. I made a quick little method that does what I need it to do but I'm not convinced it's the best way to go about doing this sort of thing. I suspect there's probably a handy regular expression I can use to do it in only a couple of lines. Here's what I got:

def remove_breaks(text)  
    if text != nil and text != ""
        text.strip!

        index = text.rindex("<br />")

        while index != nil and index == text.length - 6
            text = text[0, text.length - 6]

            text.strip!

            index = text.rindex("<br />")
        end

        text.strip!

        index = text.index("<br />")

        while index != nil and index == 0
            text = test[6, text.length]

            text.strip!

            index = text.index("<br />")
        end
    end

    return text
end

Now the "<br />" could really be anything, and it'd probably be more useful to make a general use function that takes as an argument the string that needs to be stripped from the beginning and end.

I'm open to any suggestions on how to make this cleaner because this just seems like it can be improved.

A: 

use replace method instead

str.replace("<br/>", "")
Salil
Unfortunately ruby's string replace doesn't work that way, according http://ruby-doc.org/core/classes/String.html#M000786 replace just replaces the entire string with whatever the argument is. Obviously that's not what I want. And even if it did work that way I need to to ONLY replace "<br />"s at the beginning and end of the string, but not touch any that are in the middle. For example remove_breaks(" <br /> <br /> <br />I want to keep <br /> all of this stuff here.<br />")should return "I want to keep <br /> all of this stuff here."
seaneshbaugh
+2  A: 

gsub can take a regular expression:

text.gsub!(/(<br \/>\s*)*$/, '')
text.gsub!(/^(\s*<br \/>)*/, '')
text.strip!
fgb
Thanks! This is best suited for what I need for now.
seaneshbaugh
+2  A: 
class String
    def strip_this!(t)
        # Removes leading and trailing occurrences of t
        # from the string, plus surrounding whitespace.
        t = Regexp.escape(t)
        sub!(/^(\s* #{t} \s*)+  /x, '')
        sub!(/ (\s* #{t} \s*)+ $/x, '')
    end
end

# For example.
str = ' <br /> <br /><br />    foo bar <br />    <br />   '
str.strip_this!('<br />')
p str                     # => 'foo bar'
FM
fgb's answer is slightly better for what I'm trying to do. Extending String for something I only use in two places in an entire Rails app seems a bit much. However this is definitely a very good general purpose solution and I can tell I'll be using it in the future. It's a shame I can't mark two answers as correct because this is really cool.
seaneshbaugh
A: 
def remove_breaks(text)
  text.gsub((%r{^\s*<br />|<br />\s*$}, '')
end

%r{...} is another way to specify a regular expression. The advantage of %r is that you can pick your own delimeter. Using {} for the delimiters means not having to escape the /'s.

Wayne Conrad