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
Solved

Problem with the Data

Posted on 2011-09-11
11
549 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
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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 333 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 167 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 333 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
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…

839 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