Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you create a JavaScript string without using ' or " quotes?

I have a JS file with some XML in it, where the XML is supposed to get converted to a word by the server.

E.g.

var ip = "<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>";

This gets converted to:

var ip = "192.168.0.0";

However, in case the server doesn't work as intended, I don't want there to be a syntax error, and this is VERY important. Currently there would be a syntax error because the language uses both types of quotes. I can't think of a way to get around this, but perhaps there's another way to do quotes in JavaScript? Or to create a string?

For example, in Python I'd use triple quotes:

ip = """<lang:cond><lang:when test="$(VAR{'ip_addr'})">$(VAR{'ip_addr'})</lang:when></lang:cond>"""

Anyone have a bright idea?

like image 971
Jeff Avatar asked Jan 24 '12 01:01

Jeff


People also ask

Does a string need quotes?

Strings in yaml only need quotation if (the beginning of) the value can be misinterpreted as a data type or the value contains a ":" (because it could get misinterpreted as key).

How do you skip quotes in JavaScript?

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.

Do string variables need quotation marks?

String values must be enclosed in single or double quote marks. For variables such as [$END_USER$ sting values enclosed in double quote marks are resolved, whereas those enclosed in single quote marks are kept as-is.

How do you declare a string in JavaScript?

To declare variables in JavaScript, you need to use the var, let, or const keyword. Whether it is a string or a number, use the var, let, or const keyword for its declaration. But for declaring a string variable we had to put the string inside double quotes or single quotes.


1 Answers

I have had to create strings without quotes for a project as well. We were delivering executable client javascript to the browser for an internal website. The receiving end strips double and single quotes when displayed. One way I have found to get around quotes is by declaring my string as a regular expression.

var x = String(/This contains no quotes/);
x = x.substring(1, x.length-1);
x;
like image 74
Josh Avatar answered Oct 18 '22 20:10

Josh