1

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

4
  • 2
    Did you try and remove the .test() and just use: .replace(/^#[0-9A-F]{6}$/ig, () => ...) Commented Aug 8, 2020 at 8:18
  • Yep, didn't work Commented Aug 8, 2020 at 8:33
  • 1
    Try removing the ^ and $ and use Math.floor(Math.random()*16777215).toString(16) to get a new hex Commented Aug 8, 2020 at 8:37
  • Didn't work + that method of getting HEX color doesn't work for me, still can't figure out why Commented Aug 8, 2020 at 8:46

3 Answers 3

1

Replace data.replace(/^#[0-9A-F]{6}$/i.test('#AABBCC'), () => '#' + ("000000" + Math.random().toString(16).slice(2, 8).toUpperCase()).slice(-6)); with:

data.replace(/#[0-9A-F]{3,6}/ig, () => `#${Math.floor(Math.random()*16777215).toString(16)}`);

I added global flag to your regex and found a shorter way to generate a random color from here, besides removing unnecessary .test and deleting ^/$ (matches at the start of the string)

Sign up to request clarification or add additional context in comments.

9 Comments

Unfortunately it didn't work Also, before I settled for the original way of getting a random hex, I tried the method you provided, which unfortunately brought errors to my html page
Tried var result = data.replace(/#[0-9A-F]{6}/ig, () => '#' + Math.floor(Math.random()*16777215).toString(16)); Oddly only worked for the first hex in the file
I am not sure to understand, how did you get these errors if replace doesn't work? The regex should be working from my latest edit, sorry abouth that
data.replace(/#[0-9A-F]{6}/ig, Math.floor(Math.random()*16777215).toString(16)); managed to replace all HEX colors on a single random one
If you are using short hex like #FFF, change #[0-9A-F]{6} to #[0-9A-F]{3,6}
|
1

Here is my function to generate a random HEX color :

function randomHEX(){
    const hex = (Math.random()*0xFFFFFF<<0).toString(16);
    return `#${hex}`;
}

Comments

0

Generate random hex color:

function rndHex(){return'#'+('00000'+(Math.random()*(1<<24)|0).toString(16)).slice(-6)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.