1

I've tried looking around on SO and google but I can't find a solution. I'm trying to read a text file and splitting it to store it in an array. The problem is, a newline gets attached to the last element and I can't fix it. I tried using regex but it's not working.

the text file looks like this-

nodejs,express,mutler,body-parser,quack-quack,lorem-ipsum,boom

Here's the code-

module.exports = function (request) {

    var express = require('express');
    var fs = require('fs');
    var raw = '';
    var path = './' + request.file.path;
    fs.readFile(path, {
        encoding: 'utf-8'
    }, function (err, data) {
        if (!err) {
            raw = data.split(',');
            raw[raw.length - 1].replace(/(\r\n|\n|\r)/gm, '');
            //cheap hack to replace the \n but it doesn't work
            console.log(raw);
        } else {
            console.log('Error ' + err);
        }

    });
};

When I execute this, the console outputs this-

[ 'nodejs',
  'express',
  'mutler',
  'body-parser',
  'quack-quack',
  'lorem-ipsum',
  'boom\n' ]

I want to loop over that array and pass each element as argument to a function.

0 Answers0