variables as Verbatim Strings. Pls help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cortes

    #1

    variables as Verbatim Strings. Pls help

    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



  • Lasse Reichstein Nielsen

    #2
    Re: variables as Verbatim Strings. Pls help

    "Cortes" <tvhoang1980@ya hoo.com> writes:
    [color=blue]
    > 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();[/color]

    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.
    [color=blue]
    > xyz = " This is a long long text with a lof of weird characters that need
    > escape character"[/color]
    [color=blue]
    > So in order to construct a proper xyz, one way I know is to use
    > concatenation and A LOT of escape character.[/color]

    That will work. What is the problem with that?
    [color=blue]
    > My question therefore is:
    > Is there any way to get around this?[/color]

    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.getEle mentById("hidde nDiv").firstChi ld.nodeValue;
    </script>

    Be aware that some browsers will trim the surrounding whitespace around
    the text.
    [color=blue]
    > I know in PHP you have something like this (may syntactically wrong)
    >
    > var xyz = <<< Orginal text >>>[/color]

    More likely (IIRC):

    $xyz = <<EOT
    original text
    EOT
    [color=blue]
    > Anyone have any idea please help me! I will appreciate any reply. Thanks[/color]

    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
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    Working...