Javascript heredoc

后端 未结 14 1540
盖世英雄少女心
盖世英雄少女心 2020-12-02 12:55

I need something like heredoc in JavaScript. Do you have any ideas for this? I need cross-browser functionality.

I found this:

heredoc = \'\\
相关标签:
14条回答
  • 2020-12-02 13:34

    ES5 and earlier versions

    (function(){/**
    some random
    multi line
    text here
    **/}).toString().slice(15,-5);
    

    ES6 and later versions

    `some random
    multi line
    text here`
    

    result

    some random
    multi line
    text here
    
    0 讨论(0)
  • 2020-12-02 13:35

    If you have some html and jQuery at hand and the string is valid HTML, this may be useful:

    <div id="heredoc"><!--heredoc content
    with multiple lines, even 'quotes' or "double quotes",
    beware not to leave any tag open--></div>
    <script>
    var str = (function() {
       var div = jQuery('#heredoc');
       var str = div.html();
       str = str.replace(/^<\!--/, "").toString();
       str = str.replace(/-->$/, "").toString();
       return str;
    })();
    </script>
    

    If text have comments "<!-- -->" in between, it works as well, but a part of the text may be visible. Here's the fiddle: https://jsfiddle.net/hr6ar152/1/

    0 讨论(0)
提交回复
热议问题