Node.JS read a string line by line
<p>I am working with Node.Js and have the below block of code to pull a string from a Database location, strips off certain junk and sets it up to be uniform, so I can update another table with this information later.</p>

<pre><code>jsUpdateCon.query('SELECT FileContent FROM codeFile WHERE ID = ?',[msg[1]], function(err, result){
    if (err) throw err;
    str = result[0].FileContent;
    var firstInstance = str.search(/=/) + 2;
    var lastInstance = str.lastIndexOf('\\') + 1;
    str = str.substring(firstInstance, lastInstance);
    str = str.replace(/\\n\\/g, "");
    str = str.replace(/\#(.*)/g, "");
    str = str.replace(/\n\s*\n/g, "\n");
    str = str.replace(/snippet /g, "\nsnippet ");
    str = str.replace(/^(?:\t)/gm, "");

    console.log(str);
});
</code></pre>

<p>Here is a sample of the output from <code>console.log(str);</code></p>

<pre><code>snippet cfabort
&lt;cfabort&gt;

snippet cfargument
&lt;cfargument name=\"${1:ArgName}\" type=\"${2:any}\" default=\"${3:DefaultValue}\" hint=\"${4:hint about this argument}\"&gt;

snippet cfbreak
&lt;cfbreak&gt;

snippet cfcase
&lt;cfcase value=\"${1:${SELECTED_TEXT:&lt;!--- code ---&gt;}}\"&gt;
        ${0}
&lt;/cfcase&gt;

snippet cfcatch
&lt;cfcatch&gt;
        ${0}
&lt;/cfcatch&gt;

snippet cfcatch:type
&lt;cfcatch type=\"${1:any}\"&gt;
        ${0}
&lt;/cfcatch&gt;

snippet cfcomponent
&lt;cfcomponent&gt;
        ${0:&lt;!--- code ---&gt;}
&lt;/cfcomponent&gt;

snippet cfcontent
&lt;cfcontent deleteFile=\"${1:no}\" file=\"${2:filename}\" reset=\"${3:yes}\" type=\"${4:fileType}\" variable=\"${5:variableName}\"&gt;

snippet cfcontinue
&lt;cfcontinue&gt;

snippet cfdefaultcase
&lt;cfdefaultcase&gt;

snippet cfdirectory:c
&lt;cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:copy}\" destination=\"${3:destinationPath}\"&gt;

snippet cfdirectory:cr
&lt;cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:create}\" &gt;

snippet cfdirectory:d
&lt;cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:delete}\" recurse=\"${3:yes|no}\"&gt;

snippet cfdirectory:l
&lt;cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:list}\" name=\"${3:nameOfOutputSet}\" recurse=\"${4:yes|no}\" sort=\"${3:asc|desc}\" &gt;

snippet cfdirectory:r
&lt;cfdirectory directory=\"${1:pathToDirectory}\" action=\"${2:rename}\" newDirectory=\"${3:newNameforDirectory}\"&gt;

snippet cfdump
&lt;cfdump var=\"

snippet cfelse
&lt;cfelse&gt;
    ${0:&lt;!--- code ---&gt;}
</code></pre>

<p>I am now needing to parse this string to insert into a database table.</p>

<p>Any line beginning with snippet[space] (such as "snippet cfabort") -> insert into keyboardShortcuts.keyShort such as "cfabort"(removing the snippet[space]).</p>

<p>Every line after (excluding the blank line between groups) -> insert into keyboardShortcuts.snippet such as "&lt;cfabort&gt;" keeping formatting of the group.</p>

<p>And then breaking on a blank line to start the next row in the DB table with the next snippet.</p>

<p>Do I use the <a href="https://nodejs.org/api/readline.html" rel="nofollow">readline</a> from NodeJs?  This appears to only use a file stream not a string in memory.</p>

<p>Do I do use something else?  What is the best way to accomplish this?</p>

<hr>

<p>Current Working Code with Abdullah Shahin's answer:</p>

<pre><code>jsUpdateCon.query('SELECT FileContent FROM codeFile WHERE ID = ?',[msg[1]], function(err, result){
    if (err) throw err;

    str = result[0].FileContent;
    var firstInstance = str.search(/=/) + 2;
    var lastInstance = str.lastIndexOf('\\') + 1;
    str = str.substring(firstInstance, lastInstance);
    str = str.replace(/\\"/g, "\"");
    str = str.replace(/\&lt;/g, "&amp;lt;");
    str = str.replace(/\&gt;/g, "&amp;gt;");
    str = str.replace(/\\n\\/g, "");
    str = str.replace(/^\#(.*)/gm, "");
    str = str.replace(/\n\s*\n/g, "\n");
    str = str.replace(/^(?:\t)/gm, "");
    str = str.trim();

    str = str.replace(/^\s*[\r\n]/gm,"");

    var object = {};
    var tempArray = str.split("\n");
    var currentObj = "";
    for(var i=0; i&lt;tempArray.length;i++){
        if(/snippet /g.test(tempArray[i])){
            currentObj = tempArray[i].replace(/snippet /g, "");
            object[currentObj] = "";
        } else {
            object[currentObj] += tempArray[i];
        }
    }
});
</code></pre>