Javascript Multiline Strings (HEREDOC equivalent) - solution


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Javascript Multiline Strings (HEREDOC equivalent) - solution

  1. #1
    Join Date
    Jul 2006
    Posts
    2

    Javascript Multiline Strings (HEREDOC equivalent) - solution

    This is my post from another forum - I've found a way to do multiline strings
    in Javascript without having to escape special characters, etc.

    Here is the post:

    I've seen posts on the Net asking if you can do multiline
    strings in Javascript code like you can in PHP with the HEREDOC syntax.

    I've seen solutions that do it by putting the text in textarea element but haven't seen any where it's done it code.

    Here's my way of doing it in code by using anonymous functions and the /* */ comment tags.

    Put your multiline string between the /* */ comment tags in an anonymous function and call the GetMultiLine function to get just your string from the anonymous function. Now you can do multiline strings without having to escape quotes, new lines, etc!

    I've been a bit lazy to go figure out the regular expression to
    do the "replaces" in one line and optimise the function but I'll do that sometime.

    TODO: We could insert {x} in the multiline string where x is a variable/expression and replace that part of the string with the actual value by doing an eval, etc.
    This way we could do what we can do in PHP where we can insert variables in strings.

    Let me know what yawl think.........

    <script>

    function GetMultiLine(sStr)
    {
    var s = sStr;

    s = s.replace(/function/, '');
    s = s.replace(/\(/, '');
    s = s.replace(/\)/, '');
    s = s.replace(/\{/, '');
    s = s.replace(/\}/, '', -1);
    s = s.replace(/\//, '');
    s = s.replace(/\*/, '');
    s = s.replace(/\//, '', -1);
    s = s.replace(/\*/, '', -1);

    return s;
    }

    var f = function() {
    /*
    the
    quick
    brown
    fox
    jumps
    over
    the lazy dog
    */
    };

    alert(GetMultiLine(new String(f)));

    </script>

  2. #2
    Join Date
    Aug 2007
    Posts
    1

    Smile

    this leaves a trailing slash at the end of the string and also causes removal of the first slash if you use HTML in the multiline variable.

    I rewrote the function so that you can write HTML from the javascript.
    Important:
    you have to write the first and last line of the variable function like so:
    Code:
    function(){
    and so:
    */};
    each without space and in 1 line.

    the result:

    <script type="text/javascript">
    function GetMultiLine(sStr){
    var s = sStr;
    s = s.replace(/function\(\){/, '');
    s = s.replace(/\*\/}/, '');
    s = s.replace(/\//, '', -1);
    s = s.replace(/\*/, '', -1);
    return s;
    }

    var f = function(){
    /*
    <html>
    multi
    line
    stuff
    </html>
    */};



    document.write(GetMultiLine(new String(f)));


    </script>
    Last edited by digitil; 08-07-2007 at 05:10 PM.

  3. #3
    Join Date
    Sep 2007
    Posts
    1

    Javascript Multiline Strings (HEREDOC equivalent) - solution

    This is a neat idea, and lack of multiline string support via some sort of 'heredoc' or other syntax (e.g. Python's triple quote) is really a rather astounding shortcoming in JS given it's close association with HTML. (i.e., I think a hack like this is really needed and could be very useful.)

    Unfortunately, this solution appears to be highly non-portable.

    Under FireFox 2.0.0.6, such comments appear to be "pre-processed" out. This:

    Code:
    var f = function(){
        var s = "Hello?";
    /*
    this is a multi line
    string with embedded
    newlines of its own
    */};
    
    document.write(new String(f));
    Will yield this:

    Code:
    function () {
        var s = "Hello?";
    }

    Under IE 7.0, I don't even get that: It will print absolutely nothing.
    Can anyone else verify that's what happens on those two browsers?
    Anyone understand the reason behind that and have a workaround?

    I don't have easy access to older browsers to check prior behavior.

  4. #4
    Join Date
    May 2008
    Posts
    1
    Right, this solution does not work with Mozilla. But it worked with IE 7. Is there any alternative??

  5. #5
    Join Date
    Jan 2009
    Posts
    1

    Thumbs up Les vrais heredoc

    Hi,

    Personnellement, je préfère tout simplement utiliser la fonction adhoc du capitaine :
    Code:
    const monTexteWithReturns="\
    Une ligne\n\
    Une autre ligne\n\
    Encore une ligne\
    .";
    /* Note : les "\n" sont des retours chariot, of course */

    C'est-à-dire que :
    Code:
    monDiv.innerHTML=monTexteWithReturns.replace(/\n/g,"<br />");
    // … affichera :
    Une ligne
    Une autre ligne
    Encore une ligne.
    
    /* Remarquer le "." qui n'était pas à la ligne */
    Code:
    const monTexteWithoutReturns="\
    Ceci \
    est \
    une \
    simple \
    Phrase.\
    ";
    
    // … affichera :
    
    Ceci est une simple Phrase.
    Pourquoi faire plus compliqué ?…

    Bien à vous,

    Fil

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    In English please.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  7. #7
    Join Date
    Feb 2009
    Posts
    2

    Thumbs up Translation & correction

    Although I don't speak that foreign language, what FilDePantin was rather straightforward:

    In Javascript, if a line ends with a backslash, the next line is viewed as a continuation.

    So the following multi-line string will work in any browser:
    Code:
    var myString = "\
    one, \
    two, \
    three";
    
    Alert(myString);
    //alerts "one, two, three"
    If you want the browser to display the string in multiple lines, add line breaks to each line:
    Code:
    var myString = "\
    one, \r\n\
    two, \r\n\
    three";
    
    Alert(myString);
    //alerts:
    one,
    two, 
    three
    As an aside, it is possible to store the string as text inside a div, which is set to display:none, and when needed a regex can collapse it to a string:
    Code:
    <div style="display:none" id="myString">
    one <br/>
    two <br/> 
    three 
    </div>
    <script>
    var myString = document.getElementById('myString').innerTEXT.replace('<br />', '');
    
    alert(myString);
    //alerts:
    one two three
    </script>
    Hopes that helps the other Americans.
    If I got it wrong (again, I am no speaking language he using), feel free to add/correct

Similar Threads

  1. Replies: 0
    Last Post: 03-18-2005, 03:30 AM
  2. Replies: 90
    Last Post: 04-17-2001, 01:45 AM
  3. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center