Replacing " with \" in ASP - javascript

As the title suggests, I am trying to replacing a " with the escape character \". The reason I am doing this is because there is a quote in my string that represents inches and this string needs to be run through javascript code.
However, when I run my string in the javascript code, it only reads up to the inches character and forgets the rest of the string.
Any and all help is greaty appreciated.

A function like this will escape \, " and ':
function AddSlashes(str)
AddSlashes = replace(str,"\","\\")
AddSlashes = replace(AddSlashes,"'","\'")
AddSlashes = replace(AddSlashes,chr(34),"\" & chr(34))
end function
Source

If you define like this var s = 'some string';, the quote is work ok, but if you do like var s = "some string";, you should replace the quote to \", so the code is var s = "some string \ " with the quote";
also you need to replace the Vbcrlf and vbcr and vblf before the JS code

Related

How to fix 'missing) after argument list' error in Javascript

Why is this giving me this error?
<script>
function loadImg() {
var imageChosen = document.getElementById('imageChosen').value.replace("C:\fakepath\","");
alert(imageChosen);
document.getElementById('image').src = imageChosen;
}
</script>
I expect the image with id "image" to show the chosen image.
The value in your call to replace() is not escaped properly.
The value should instead be:
"C:\\fakepath\\",""
Read more about escaping strings here
The problem is due to the escape string character \ (backslash)
When using strings in Javascript we may escape some character in the string. For example a break line (\n) or even a "(double quotes) when declaring the string or even an backslash \ need a escape.
Examples:
x = "my \\" // Will output as the same as "my \"
z = "my \"quotes\" // Will output as 'my "quotes" '

in javascript, how do I store in a variable (coming from a database) that contains both quotes and apostrophes?

Let's say I access the database with this syntax :{column_name}: and I want to store it as a string in a javascript context. It works well using var str = ':{column_name}:' until the data contains apostrophes, same with quotes because the data may contain both quotes and apostrophes. I think it's impossible. What do you think?
You can escape both quotes and apostophes by using \" and \'. For example:
var str = '\' is an apostrophe and \" is a quote';
or
var str = "\' is an apostrophe and \" is a quote";
See the section "Special Characters" on http://www.w3schools.com/js/js_strings.asp

How to escape characters in a string

Consider below response of ajax-
function validateVar(){
$.ajax({
url:"abc.do",
datatype:"text",
success:function(response){
alert(response); //"asdjakd"fsd'f'fsf"s'dfs'df"fsdf"fsfsf""
}
});
}
I want to escape all occurrences of quotes in the response.
JSFIDDLE: https://jsfiddle.net/ashwyn/3w0aj5z7/
You can use the backslash to escape a character \. So to store your string in a variable we can write it like this.
var v = "asdjakd\"fsd\'f\'fsf\"s\'dfs\'df\"fsdf\"fsfsf";
You can't programmatically escape a string to make it valid JavaScript.
It has to be valid JavaScript to get through the JavaScript parser before it can be modified by JavaScript.
You have to fix it in the source code.
The \ character starts an escape sequence in a JavaScript string literal:
var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";
You can use backslash to solve this
You need to add backslash for same type inverted commas(single or double).
function validateVar(){
var v = "asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf";
alert(v);
}
Update the fiddle as well, check here :: https://jsfiddle.net/3w0aj5z7/1/
You need to escape the double quotationmark " with backslash \
Your string should look like this:
"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf"
You can include the string between '' or "" .
Then you must say to the variable where quotes aren't the closing ones with \ before it.
In this case:
var v = ' "asdjakd"fsd\'f\'fsf"s\'dfs\'df"fsdf"fsfsf" ';
before ' quote
or
var v = " \"asdjakd\"fsd'f'fsf\"s'dfs'df\"fsdf\"fsfsf\" ";
before " quotes

How to add single quote in the variable in Javascript?

I have variable var str as following:
var str = <option value="1">tea</option>;
I would like to make it as below
var quote_str = '<option value="1">tea</option>;'
Is there anyone can help me? Thanks in advance!
Edit:
I have tried the following code,however, it's not correct.
var quote_str = 'str';
I think that you want the semicolon outside the string literal:
var quote_str = '<option value="1">tea</option>';
If you mean that you want apostrophe characters inside the string also, you can use \' to put an apostrophe in a string delimited by apostrophes:
var quote_str = '\'<option value="1">tea</option>\'';
You can also use quotation marks to delimit the string. Then you don't have to escape the apostrophes, but you have to escape the quotation marks:
var quote_str = "'<option value=\"1\">tea</option>'";
If you already have a string, and want to add apostrophes around it, you concatenate strings:
var quote_str = "'" + str + "'";
Escape each single quote with a back-slash:
var quote_str = '\'<option value="1">tea</option>;\''
…or wrap the string in quotes of a different kind (i.e. double quotes), but be sure to escape the inner double quotes as to not unintentionally close the string:
var quote_str = "'<option value=\"1\">tea</option>;'"
late update: now we have template literals, so the whole thing becomes a breeze:
var quote_str = `'<option value="1">tea</option>;'`
You can escape characters in Javascript with the \. If that's your issue
We can use the backslash () escape character to prevent JavaScript from interpreting a quote as the end of the string.
The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.
Using this method, we can use apostrophes in strings built with ".
'We\'re safely using an apostrophe in single quotes.' We can also use quotation marks in strings built with ".
"Then he said, \"Hello, World!\"";
In my case, i'm unable to use the notation of ${} in rendered Javascript inside Python Mako Templates as it's already using ${} for rendering variables in Mako:
# mako template somewhere
var quote_str = `'${str}'`;
So i just wrote a small function:
# app.js ( a real Javascript file )
function singlequote(text) {
return `'${text}'`;
}
And then I use:
# mako template somewhere
var quote_str = singlequote(str);
# So i'm able to also use something like:
let btn = '<button type="button" onclick="update(' + singlequote(myid) + "," + singlequote(mystate) + ')"> Update </button>';

to replace " " with

this is my javascript code:
mystring = "this is sample";
nestring = mystring.replace(/ /g," ");
I need nestring ouput "this is sample".
but with the code above, nestring = "this is sample"
how can I replace space( ) with &nbsp( )?
Many thanks!
re:
I embed svg in html. But ie, chrome do not support xml:space=preserve, firefox does. By replace " " with &nbsp, multiple " " will not condense to one " ".
You could use the Unicode:
nestring = mystring.replace(/ /g, "\u00a0");
But your example did exactly what you told it to.
If you want to replace the characters with character code 32 (0x20) with character with character code 160 (0xA0), you can use the fromCharCode method to create the string to replace with:
nestring = mystring.replace(/ /g, String.fromCharCode(160));
Your code works fine, but if you are writing the result to the document you will only see the spaces because they are HTML encoded.
If you alert(nestring); you will see that the spaces are replaced.

Categories

Resources