How can I replace first N occurrences of many whitespaces and tabs in the following string:
07/12/2017 11:01 AM 21523 filename with
You could take a counter and decrement it.
var string = '07/12/2017 11:01 AM 21523 filename with s p a c e s.js',
n = 4,
result = string.replace(/\s+/g, s => n ? (n--, '|') : s);
console.log(result);
You could replace the ternary expression with one with logical AND and OR.
var string = '07/12/2017 11:01 AM 21523 filename with s p a c e s.js',
n = 4,
result = string.replace(/\s+/g, s => n && n-- && '|' || s);
console.log(result);