0

I have a javascript variable which hold the value taken from somewhere else(lets say from a API call), taken string is given bellow,

He's the reason for this

I assign this string to a variable name 'sample'. But when I print it, it doesn't work since the string has " ' " character. I want to add " \ " before the " ' " character. I tried using this,

var sample = (get the string from a api call).replace(/'/g,"\\\'");

But it doesn't work?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
Roshanck
  • 2,220
  • 8
  • 41
  • 56
  • 3
    Define "print it". While you are working within JS, having a `'` in a string should not cause you any problems. – Quentin Aug 16 '12 at 14:31
  • 1
    In addition to "print it", please also define "doesn't work". – apsillers Aug 16 '12 at 14:33
  • Actually the problem is, I pass this variable to another php file using GET method. I can't print it there. That's the problem – Roshanck Aug 16 '12 at 14:34
  • I think he means he assigned a string with `'`, but the string itself contained `'`, which gives an error if they aren't escaped. I actually had the same problem at work yesterday, and just assigned the string using `"` instead. – Daniel Aug 16 '12 at 14:34
  • @Roshanck — So you are passing it over HTTP. How are you sending doing that? How are you using it within that PHP? What do you mean by print (call the PHP `print` method? Call some method that outputs a hard copy to a printer connected to the server?)? – Quentin Aug 16 '12 at 14:36
  • I take the string from a API call, so how to add " before and after the string – Roshanck Aug 16 '12 at 14:37
  • When you say `(get the string from a api call)` do you mean `` ? – Quentin Aug 16 '12 at 14:39
  • @ Quentin-- in my javascript file I use window.location.href = "test.php?detail="+sample; to send the data. In my test.php, I use $detail = $_GET["detail"]; and echo $detail; to print it. Is there a good way to do this? – Roshanck Aug 16 '12 at 14:42
  • Show your actual code, both your PHP code and the code produced by PHP that is sent to the browser (hint: View Source). Your usage of `(get the string from a api call)` is hiding details critical to helping you. – Phrogz Aug 16 '12 at 14:46
  • possible duplicate of [Pass a PHP string to a Javascript variable (including escaping newlines)](http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines) – Phrogz Aug 16 '12 at 14:52

3 Answers3

1

in my javascript file I use window.location.href = "test.php?detail="+sample; to send the data.

Use encodeURIComponent to escape a string for inserting into a URI.

In my test.php, I use $detail = $_GET["detail"]; and echo $detail; to print it.

If you are printing it into HTML then use htmlspecialcharsto make it safe.

If you are printing it into JavaScript then use json_encode to make it safe.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

You're overdoing the escape characters:

var sample = (get the string from a api call).replace(/'/g,"\\'");

Is enough, a single quote, delimited by double quotes needn't be escaped, so just escape one backslash.
A sidenote, though: if the string you're checking is a return value, the single quotes shouldn't be a problem (if they are, the api code would break before returning the string). If you really really really want to be extra-super-mega-sure and the string is long:

var sample = (get the string from a api call).split('\'').join('\\\'');
//or (to avoid confusion with all that escaping
var sample = (get the string from a api call).split("'").join("\\'");

Splitting is faster for longer strings (not short strings, as the array constructor is called, an array-object is created, looped,...)

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
0

Presumably the problem is with (get the string from a api call). If you have some server-side code (PHP?) like this:

var sample = <?php echo $mystring ?>.replace(…);

…and it produces output sent to the browser like this:

var sample = 'my dad's car'.replace(…);

…then your server-side code has produced syntatically-invalid JavaScript that cannot be fixed by more JavaScript. Instead you need to fix it on the server, something like:

var sample = <?php echo json_encode($mystring); ?>;

It's impossible to help you further without your actual code details, however.

Phrogz
  • 296,393
  • 112
  • 651
  • 745