In order to strip unprintable characters from strings in Ruby, you can use the following regex.
"your_string".gsub!(/[^[:print:]]/, '')
Question
I have a string within my database that contains this sample substring.
string = "\357\277\275\357\277\275"
When I try to convert this to JSON, I get a lot of these bad boys (since they are non-ASCII characters).
�
Then, when jQuery tries to parse the JSON, it just craps out and gives me a SyntaxError: Unexpected Token
Here are three possible solutions.
I am fine with any of these, but don't know how to go about them. Thoughts?
Solution 2
In order to strip unprintable characters from strings in Ruby, you can use the following regex.
"your_string".gsub!(/[^[:print:]]/, '')
OTHER TIPS
"\357\277\275"
is the ascii octal representation of the Replacement Character(�). This indicates that when you converted to JSON there were non-ascii characters in your string. Ideally, you would want to identity how non-ascii characters are getting into your data upstream but the easy fix is to just delete them:
clean_string = "absr\357\277\275/xyz".gsub("\357\277\275","")
#=> "absr/xyz"