Recently I've asked on how to replace a thousand of #FFF with a random HEX color. The solution was to run this code:
var fs = require('fs')
fs.readFile('stars.css', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var result = data.replace(/#FFF/g, () => '#' + ("000000" + Math.random().toString(16).slice(2, 8).toUpperCase()).slice(-6));
fs.writeFile('stars.css', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
I'm looking for a way to detect any HEX color within the file, and replace it with a new random HEX color. Here's what I tried:
var result = data.replace(/^#[0-9A-F]{6}$/i.test('#AABBCC'), () => '#' + ("000000" + Math.random().toString(16).slice>
Also, ("000000" + Math.random().toString(16).slice(2, 8).toUpperCase()).slice(-6) is the only way for me to get HEX color, as Math.floor(Math.random()*16777215).toString(16) method throws an error on my webpage
.test()and just use:.replace(/^#[0-9A-F]{6}$/ig, () => ...)^and$and useMath.floor(Math.random()*16777215).toString(16)to get a new hex