# Gsub html sanitizer

**URL:** <https://www.ruby-forum.com/t/gsub-html-sanitizer/147402>\
**Category:** Ruby\
**Created:** [September 1, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402 "2008-09-01T14:14:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/1 "2008-09-01T14:14:37Z")

</div>

I’m new to Ruby and a bit confused about how gsub works. I’ve read the  
documentation, searched ruby-forum, and tried numerous google searches,  
but can’t seem to figure out how to do something that should be simple.

This is just an example, but say I have a string “apple cat flower”. I’d  
like to replace all instances of the pattern “apple” with the string  
“fruit,” “cat” with the string “animal,” and “flower” with the string  
“plant.” Meaning I have more than 1 pattern to replace, each with a  
specific string to replace.

So I want “apple cat flower” to be converted into “fruit animal plant”.

I hope this isn’t too confusing. Help is appreciated 🙂

---

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/2 "2008-09-01T14:25:37Z")

</div>

You could just chain this up to make it neater?

orig=“apple cat flower”  
orig.gsub(“apple”,“fruit”).gsub(“cat”,“animal”).gsub(“flower”,“plant”)

=\> “fruit animal plant”

There might be a ‘smarter’ way of doing this: I’m only just learning  
Ruby myself , but I know from other domains (“gsub” is an old ‘awk’  
command for instance) that generally speaking when doing regex  
search/replace stuff you would tend to actually do this in “n”  
iterations rather than coming up with a complex ‘or’ regex…

Also, I know in regex you can use the ‘&’ character to mean “The  
resulting matched character(s)” - but in this case you are literally  
looking for three distinct patterns…so I’m not sure this is even a  
possibility…

The above is just my opinion about using regexes - I would certainly  
break this up into three separate gsubs: and I might be missing a trick  
or two here…so wait for other posts I would say…

You could write yourself a little ‘method’ here which took two arrays  
perhaps, and then iterate over one with the other ? dunno…

John

Chealsea S. wrote:

> I’m new to Ruby and a bit confused about how gsub works. I’ve read the  
> documentation, searched ruby-forum, and tried numerous google searches,  
> but can’t seem to figure out how to do something that should be simple.
> 
> This is just an example, but say I have a string “apple cat flower”. I’d  
> like to replace all instances of the pattern “apple” with the string  
> “fruit,” “cat” with the string “animal,” and “flower” with the string  
> “plant.” Meaning I have more than 1 pattern to replace, each with a  
> specific string to replace.
> 
> So I want “apple cat flower” to be converted into “fruit animal plant”.
> 
> I hope this isn’t too confusing. Help is appreciated 🙂

---

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/3 "2008-09-01T14:26:50Z")

</div>

[TYPO correction: I didn’t mean to quote ‘method’ in my reply…I was in  
the middle of editing and pressed send to quick , sorry for any  
confusion]

---

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/4 "2008-09-01T14:31:19Z")

</div>

2008/9/1 Chealsea S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> So I want “apple cat flower” to be converted into “fruit animal plant”.
> 
> I hope this isn’t too confusing. Help is appreciated 🙂

As long as the number of strings is not too large you can use  
Regexp.union:

irb(main):004:0\> replacements =  
{“apple”=\>“fruit”,“cat”=\>“animal”,“flower”=\>“plant”}  
=\> {“cat”=\>“animal”, “apple”=\>“fruit”, “flower”=\>“plant”}  
irb(main):005:0\> Regexp.union replacements.keys  
=\> /cat|apple|flower/  
irb(main):006:0\> “apple cat  
flower”.gsub(Regexp.union(replacements.keys)) {|m| replacements[m]}  
=\> “fruit animal plant”

If the number increases you probably rather want something like this

irb(main):007:0\> “apple cat flower”.gsub(/\w+/) {|m| replacements[m] ||  
m}  
=\> “fruit animal plant”

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:50pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/5 "2008-09-01T14:50:49Z")

</div>

Thanks everyone for the replies. They were extremely useful.

---

<div class="post-metadata">

**Author:** ![chelseas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@chelseas](https://www.ruby-forum.com/u/chelseas)\
**Post date:** [September 1, 2008, 2:31pm UTC](https://www.ruby-forum.com/t/gsub-html-sanitizer/147402/6 "2008-09-01T14:31:55Z")

</div>

On Mon, Sep 1, 2008 at 2:09 PM, Chealsea S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I want “apple cat flower” to be converted into “fruit animal plant”.
> 
> I hope this isn’t too confusing. Help is appreciated 🙂

If you don’t mind a loop, you could gsub each pattern one at a time.

irb(main):002:0\> patterns = {“apple” =\> “fruit”, “cat” =\> “animal”,  
“flower” =\> “plant”}  
=\> {“cat”=\>“animal”, “apple”=\>“fruit”, “flower”=\>“plant”}  
irb(main):003:0\> s = “apple cat flower”  
=\> “apple cat flower”  
irb(main):004:0\> patterns.each {|p,r| s.gsub!(p, r)}  
=\> {“cat”=\>“animal”, “apple”=\>“fruit”, “flower”=\>“plant”}  
irb(main):005:0\> s  
=\> “fruit animal plant”

You could also build a big regexp and let gsub do the loop:

irb(main):008:0\> s = “apple cat flower”  
=\> “apple cat flower”  
irb(main):009:0\> re = Regexp.new(“(#{patterns.keys.join(”|“)})”)  
=\> /(cat|apple|flower)/  
irb(main):010:0\> s.gsub(re) {patterns[$1]}  
=\> “fruit animal plant”

Hope this helps,

Jesus.
