ruby - Replacing unicode linebreaks with BR -


in xml files, there unicode line breaks shown in screenshot. use link see screenshot

bigger screenshot

the 2 dots after "minds." line break. i've googled , tried know replace them ruby (1.8) without luck.

here's code (with different tries of unicodes), maybe me.

def formatedbody   t = self.body.gsub("\u000a","<br/>")   t = t.gsub("\u000d","<br/>")   t = t.gsub("\u0009","<br/>")   t = t.gsub("\u000c","<br/>")   t = t.gsub("\u0085","<br/>")   t = t.gsub("\u2028","<br/>")   t = t.gsub("\u2029","<br/>")   t = t.gsub(/0a\0a/u,"<br/>")   return t end 

the 2 0x0a values hex representation of line-feeds. regular ol' ascii line feeds, aka "\n\n" in string.

so, t = t.gsub(/\n/, "<br/>") should work.

t = "foo\u000d\u0009\u000c\u0085\u2028\u2029\nbar" p t  t = t.gsub(/\u000d|\u0009|\u000c|\u0085|\u2028|\u2029|\n/,"<br/>") puts t 

you can replace list of or'd characters with:

t = t.gsub(/[\u000d\u0009\u000c\u0085\u2028\u2029\n]/,"<br/>") 

either way, output like:

"foo\r\t\f\u2028\u2029\nbar" foo<br/><br/><br/><br/><br/><br/><br/>bar 

the reason

t = t.gsub(/0a\0a/u,"<br/>") 

doesn't work regex not correct.

t = t.gsub(/\x0a/,"<br/>") 

is alternate way of defining:

t = t.gsub(/\n/,"<br/>") 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -