RegEx stuff

a = [[“a^b^^”], [“c&def”]]
b= []

a.to_s.each_byte { |byte| (b.push(byte.chr) if !b.include?(byte.chr)) if
byte.chr.match(/\W+/) }

b.each do |c|
a=a.to_s.gsub(c, “\#{c}”).to_a
end

p a

=> [“a\^b\^\^c&def”]

How come the & does not get subbed with ‘\&’ ??

I’m basically trying to substitute any special character with
‘\#{special_character}’

THANKS!

On Jul 16, 1:08 pm, Justin To [email protected] wrote:

p a
Posted viahttp://www.ruby-forum.com/.
try
a=a.to_s.gsub(c, “\\#{c}”).to_a

“\” will present as ''
‘&’ will present as ‘&’

From: Justin To [mailto:[email protected]]

=> [“a\^b\^\^c&def”]

How come the & does not get subbed with ‘\&’ ??

I’m basically trying to substitute any special character with

‘\#{special_character}’

i’m a block fan, so,

irb(main):048:0> b.each do |c|
irb(main):049:1* a=a.to_s.gsub(c){“\#{c}”}.to_a
irb(main):050:1> end
=> [“^”, “&”]

irb(main):052:0> a
=> [“a\^b\^\^c\&def”]

using block form of gsub wont give you problems on escaping the escape…

kind regards -botp

Great thanks!