variables as Verbatim Strings. Pls help

C

Cortes

hi all,

I have a variable, let's call it xyz, as a string. I want xyz to be a string
which I copied from a website that has a lot of back slashes, forward
slashes, single quotes, double quotes....

var xyz = new String();
xyz = " This is a long long text with a lof of weird characters that need
escape character"


So in order to construct a proper xyz, one way I know is to use
concatenation and A LOT of
escape character.

My question therefore is:
Is there any way to get around this?

I know in PHP you have something like this (may syntactically wrong)

var xyz = <<< Orginal text >>>

Anyone have any idea please help me! I will appreciate any reply. Thanks
 
L

Lasse Reichstein Nielsen

Cortes said:
I have a variable, let's call it xyz, as a string. I want xyz to be a string
which I copied from a website that has a lot of back slashes, forward
slashes, single quotes, double quotes....

var xyz = new String();

This assignment is completely superflous. It does nothing except create a
String object (something you will probably never need to do) and store
it in a variable that is then immediately overwritten. Just:
var xyz;
will do.
xyz = " This is a long long text with a lof of weird characters that need
escape character"
So in order to construct a proper xyz, one way I know is to use
concatenation and A LOT of escape character.

That will work. What is the problem with that?
My question therefore is:
Is there any way to get around this?

There is no simpler way to enter strings than as string literals.
What you can do, depending on where your code needs to be used, is
to have the text as part of an HTML page and use DOM methods to extract it:

<div style="display:none;" id="hiddenDiv">
Very" long' string! of \something foo'bar'ed.
</div>
<script type="text/javascript">
var xyz = document.getElementById("hiddenDiv").firstChild.nodeValue;
</script>

Be aware that some browsers will trim the surrounding whitespace around
the text.
I know in PHP you have something like this (may syntactically wrong)

var xyz = <<< Orginal text >>>

More likely (IIRC):

$xyz = <<EOT
original text
EOT
Anyone have any idea please help me! I will appreciate any reply. Thanks

There is no "here text" constuction in Javascript, like there is in
PHP, Perl and sh. Either get the text from outside the script or embed
it using string literals.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top