1

I have this code

var fs = require('fs');

fs.readFile('data.txt', 'utf8', function(err, data) {
    if (err) throw err;
    console.log(data.replace('\r\n', ':'));
});

replace line breaks with ":"

Is working, but only for the first occurrence.

How can I do it to replace for all occurrences? Thank you so much

Monikhe
  • 35
  • 3

1 Answers1

0

try

var str = "This is sample text for a sample replace";  
str = str.replace(/sample/g, "lovely");
console.log(str);

//string replace n times 
let logLine ="This is sample text sample for a sample replace sample";  
var N = 2;
while(--N + 1){
  logLine = logLine.replace('sample', "lovely");
}
console.log(logLine);
Mohammad Ali Rony
  • 4,695
  • 3
  • 19
  • 33