I'm having some RegEx trouble, but I am aiming for this snippet of code to read an input such as "background-colour:red;"
, validate the syntax, then take "background-colour" and "red" into an array. It currently returns ["background-colour"]
, but not red, and I've spent around an hour to try and figure it out. Could anyone help or point me in the right direction? I've managed to get simple ones like "opacity:1;"
and "colour:red;"
to work but the hyphen has thrown a wrench in it.
let ed = document.getElementById("editor").innerText;
let dest = document.getElementById("destination");
//irrelevent code stripped here
regex3 = new RegExp(/^[a-zA-Z]+-[a-zA-Z]+:[a-zA-Z]+;$/i);
//irrelevent code stripped here
else if (regex3.test(ed)) {
console.log("valid input");
let pattern = /\w+\-\w+/g;
let temp = ed.match(pattern);
console.log(temp);
let att = temp[0];
let quant = String(temp[1]);
console.log(att);
console.log(temp);
dest.style.setProperty(att, quant);