?
Solved

Problem with the Data

Posted on 2011-09-11
11
Medium Priority
?
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36520728
Can you give an example of a before text and an after text?
0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36520963
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
ID: 36521244
I think \r\n will not work.

try to replace #chr(13)# and #chr(10)#
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36521652
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
ID: 36522405
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36522452
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 75

Accepted Solution

by:
käµfm³d   👽 earned 1332 total points
ID: 36522454
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 668 total points
ID: 36522531
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 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 36523580
@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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1332 total points
ID: 36530175
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 16

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 36541970
Thanks Guys
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
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…

777 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