JSFiddle - React, Tailwind, and code Playground

by cmruser

JavaScript

const randomString = (prefix) => {
  const randomChar = 'abcdefghijklmnopqrstuvwxyz'.charAt(Math.floor(Math.random() * 26));
  const randomString = Math.random().toString(16).slice(4, 9);
  const str = prefix ? `${prefix}-${randomString}` : `${randomChar}-${randomString}`;

  return str;
};

const a = randomString();
const b = randomString('a');
const c = randomString('b');

let v = /^a-[a-z][0-9a-f]{5}$/;

v = new RegExp(v);
if(v.test(b))
{
    alert('valid');
}
else
{
    alert('invalid');
}

console.log(a, b, c);