Solved

Problem with the Data

Posted on 2011-09-11
11
547 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:myselfrandhawa
  • 4
  • 4
  • 3
11 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Can you give an example of a before text and an after text?
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
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
 
LVL 3

Expert Comment

by:dagaz_de
Comment Utility
I think \r\n will not work.

try to replace #chr(13)# and #chr(10)#
0
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
well i need to skip the

/r/n/t\' etc tags and they may be repetitive, please give a valid regex for this
0
 
LVL 3

Expert Comment

by:dagaz_de
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 333 total points
Comment Utility
Slight correction:

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

Open in new window

0
 
LVL 3

Assisted Solution

by:dagaz_de
dagaz_de earned 167 total points
Comment Utility
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
 
LVL 15

Author Comment

by:myselfrandhawa
Comment Utility
@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
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 333 total points
Comment Utility
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
 
LVL 15

Author Closing Comment

by:myselfrandhawa
Comment Utility
Thanks Guys
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now