CGI.unescape and CGI.unescapeHTML don't do anything [duplicate] - javascript

This question already has answers here:
raw vs. html_safe vs. h to unescape html
(7 answers)
Closed 8 years ago.
I have a string that I receive as a response to a get request
{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}
In a js file:
$('.output').text('<%=CGI.unescape(#response.to_s)%>')
I still get the same string with &quot's and stuff. I also tried JS unescape() and it didn't do anything too. What's wrong?

CGI.escape and CGI.unescape is to handle URL-encoded string.
CGI.escape('&')
# => "%26"
CGI.unescape('%26')
# => "&"
Use CGI::unescape_html instead:
CGI.unescape_html('>')
# => ">"
body = '{"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}'
CGI.unescape_html(body)
# => {"revision"=>"r2407", "full_version"=>"2.5 [r2407]", "full_name"=>" [r2407]", "version"=>"2.5"}

I think you first need to unescape the HTML from response using CGI.unescape_html, then escape it for JavaScript with j:
$('.output').text('<%=j CGI.unescape_html(#response.to_s)%>')

Related

Replace String with value `$&' not working in javascript [duplicate]

This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)

Parse strange json string in php into js [duplicate]

This question already has an answer here:
Parse escaped JSON in PHP
(1 answer)
Closed 6 years ago.
How can I parse a string, like the following that I got from the API response:
"{\"response\":\"data\"}"
I need the "data" from the "response".
How can do that in PHP or in JS?
The response is a JSON string with quotations escaped. In JS, you can parse it using JSON.parse(myJsonString);
console.log(JSON.parse("{\"response\":\"data\"}").response);
Your question is a possible duplicate of Parse escaped JSON in PHP
First we need to remove the escape char \ using stripslashes:
$input = "{\"response\":\"data\"}";
$input = stripslashes($input);
Then you can insert the content into an array using json_decode with second parameter true (so you get an array instead of an object):
$input = json_decode($input,true);
So in a single line:
$input = json_decode(stripslashes($input),true);
You can access data and response as:
$input['data']; $input['response']
It is not clear if you need to do this in js or php. The above is the php solution.

Replace all in javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
How do I replace all occurences of "http://localhost" in a string in javascript?
I have res=res.replace(/^http\:\/\/localhost, url);, but it does not work. How do I fix it?
url is a variable and but "localhost" is a string.
UPDATE:
With the solutions below, I still get: ReferenceError: localhost is not defined. What am I missing?
UPDATE 2:
This is the (Perl) code that inserts the JS on the page:
$form .= qq|<script>res='$doc'; loc=document.location.href; url=loc.substring(0,loc.indexOf(":8080")); res=res.replace(/http\:\/\/localhost/g, url); document.location='data:text/html;charset:utf-8,' + res; </script>|;
use /g to replace every occurrence in your string
Something like this
str = str.replace(/http:\/\/localhost/g,'');
res=res.replace(/http\:\/\/localhost/g, url);
'g' indicates to replace all.

Set \ as string in JavaScript [duplicate]

This question already has answers here:
JavaScript backslash (\) in variables is causing an error
(5 answers)
Closed 7 years ago.
How do I set a \ as a string in JavaScript I am using it like this
Var x = {"\":"some value"}
But it's says that that is illegal, so then I tried it like '\' this but still the same error happened.
You can use escaping characters so add one more \ it will be escaped and become a string like this var x = {"\\":"some value"}
Just add an other slash '\' before it.

Encode special characters to pass in url and read by javascript [duplicate]

This question already has answers here:
Encode URL in JavaScript
(22 answers)
Closed 8 years ago.
I need to pass some parameters in the url and they can have special characters like ", spanish Ñ or ñ, : spaces and accents.
What is the propper way to encode them before adding to the url or in case I got in the html like that, read them?
I tried this:
arrayData[i] = pair[1].replace('+', " ").replace('%22', "\"");
But just get working with + or spaces, not both at the same time or in 2 lines:
arrayData[i] = pair[1].replace('+', " ");
arrayData[i] = pair[i].replace('%22', "\"");
You can try encodeUri Built-in function, for example
encodeURI('coño funcionó!')
Previous answer is correct. JavaScript has built in functions for fulfilling this kind of tasks.
You can try to investigate these functions in w3schools.com. Here are the links with basic information and live "Try it out" feature:
encodeURI - takes string with your characters and encodes it into plausible for url style ( encoding spaces and non ANSII chars )
decodeURI - takes encoded string and decodes it to initial state

Categories

Resources