Search code examples
javascripthtmlstringformattingmarkup

How to convert html code to a string that replaces the newlines with \n and tabs with \t


Is there an easy way to convert HTML code, that is structured in a certain way, to a single string (that can then be used in a Javascript variable). The new lines and tabs in the html code need to be converted to \n and \t so that the formatting can stay in tact.

example HTML:

<html>
    <head>
         <title>Hello World</title>
    </head>
    <body>
        <h1>Title</h1>
            <h2>Subtitle</h2>
                <p>Some text goes here</p>
    </body>
 </html>

I've converted this manually to this:

<html>\n\n\t<head>\n\t\t <title>Hello World</title>\n \t</head>\n\n \t<body>\n \t\t<h1>Title</h1>\n \t\t\t<h2>Subtitle</h2>\n \t\t\t\t<p>Some text goes here</p>\n \t</body>\n\n </html>\n

Is there an easy way to do this automatically? Because I need to convert larger chunks of HTML to this format. Thanks.


Solution

  •     function format(data) {
            var returnStr = "";
            var lines = data.split("\n");
            var block = [];
            for (var i = 0; i < lines.length; i++) {
                block = lines[i].split('\t');
                for (var j = 0; j < block.length; j++) {
                    returnStr += block[j];
                    if (block.length>1) {
                        returnStr +=  "\\t";
                    }
                };
                returnStr +=  "\\n";
            };
            return returnStr;
        }