Problem with the Data

Hi, I have the data coming from the database, and that data has the all kinds of special characters, My data is going back to the Page through JSON, I am using the JSStringFormat but still that is failing somewhere!

"#jsStringFormat(ReReplace(myString, '[\r\n\t"]+', '', 'ALL'))#"

if it encounter single quote, it will wreck the JSON!  and also how i do use the above rerplace to skip the double quotes and single quotes and replace with some other character or something should be done like htmleditformat so it should not the break the data

I Just tested with single quotes and it breaked, but i do not know if this will break on any other characters.

Please guide me Thanks

LVL 16
Gurpreet Singh RandhawaWeb DeveloperAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
Slight correction:

"#jsStringFormat(ReReplace(myString, '(["/\\\b\f\n\r\t]|\\u[a-fA-F0-9]{4})', '\\\1', 'ALL'))#"

Open in new window

0
 
käµfm³d 👽Commented:
Can you give an example of a before text and an after text?
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
like >>

This is the one feature's i have been testing!

The above line is coming from database so if it encounters the 's, it breaks the JSON which is sent to the Browser! I am not sure but there might be Other Special Characters like!

Double Quotes, Special Characters, etc
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
dagaz_deCommented:
I think \r\n will not work.

try to replace #chr(13)# and #chr(10)#
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
well i need to skip the

/r/n/t\' etc tags and they may be repetitive, please give a valid regex for this
0
 
dagaz_deCommented:
i think Regex cant handle that, try:

<cfset searchlist = '#chr(13)#,#chr(10)#,#chr(9)#,"'>
<cfset replacelist = ",,,">

#jsStringFormat(Replacelist(myString, searchlist, replacelist))#

(not tested)
0
 
käµfm³d 👽Commented:
Based on what I find here:  http://code.google.com/p/json-simple/wiki/EscapingExamples

My guess is that you would need something like:

"#jsStringFormat(ReReplace(myString, '(["/\\\b\f\n\r\t]|\u[a-fA-F0-9]{4})', '\\\1', 'ALL'))#"

Open in new window

0
 
dagaz_deConnect With a Mentor Commented:
or combine it but do the \n\r\t like i did above - this works definitiv for the tabs and newlines!


<cfset myString = "Hello      I'd like to             have some breaks here
in my String">


<cfset searchlist = '#chr(13)#,#chr(10)#,#chr(9)#,"'>
<cfset replacelist = ",,,">

<cfoutput>#ReReplace(Replacelist(myString, searchlist, replacelist), "[']+", "", "ALL")#</cfoutput>
0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
@dagaz_de

Thank for your code, but an u explain a bit, is seems doing toomany thigs in a singe command!, i do want to replace comma as it does not break json!

@ kaufmed  Can u please explain your code what strings or specil characters in handles and how!

Thanks
0
 
käµfm³d 👽Connect With a Mentor Commented:
Can u please explain your code what strings or specil characters in handles and how!
Sure, but I'll just focus on the ReReplace. I assume you know what jsStringFormat does since you seemed to have used it.

Given:

ReReplace(myString, '(["/\\\b\f\n\r\t]|\\u[a-fA-F0-9]{4})', '\\\1', 'ALL')

You are searching myString using the pattern (["/\\\b\f\n\r\t]|\\u[a-fA-F0-9]{4}). You are searching for ALL occurrences of things that match the pattern. In this case, the pattern tries to find either a single, special character with ["/\\\b\f\n\r\t], or ( | ) a unicode encoding of a character with \\u[a-fA-F0-9]{4}. Each occurrence that is found is prepended with an extra backslash using \\\1.

As far as what the pattern entails:

#####  FIND  #####
(                  - Start of capturing parens
[                  - Start of character group (i.e. find any character listed)
"/\\\b\f\n\r\t     - Any of these characters: double-quote ( " ), forward slash ( / ), backslash ( \\ ), bell ( \b ), form feed ( \f ), newline ( \n ), carriage return ( \r ), and tab ( \t )
]                  - End of character group
|                  - Alternation (i.e. regex OR)
\\u[a-fA-F0-9]{4}  - Match a unicode encoding (e.g. \u00EF)
)                  - End of capturing parens

#####  REPLACE  #####
\\                 - Backslash; Needs to be doubled to prevent interpreting as an escape for what follows
\1                 - Backreference to the capture group in the FIND pattern (i.e. everything inside parens)

Open in new window

0
 
Gurpreet Singh RandhawaWeb DeveloperAuthor Commented:
Thanks Guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.