I'm trying to get some filter properties of a DOM element, such as opacity, blur, etc. At the moment I'm splitting the string and then trying to match the string tokens against known filter types using regex. My question:
I'm having trouble with the logical OR in the regex below - I'm trying to match "opacity(0.234) or opacity(1)" for example.
var re1="(opacity)"; // word "opacity"
var re2="(\\()"; // single character '('
var re3 = "(0(\.\d+)?)" //0, with possible decimals after
var re4 = "(1)" //single character '1'
var re3or4 = "("+re3+"|"+re4+")"; //logical or
var re5 = "(\\))"; //single character ')'
var reg = new RegExp(re1+re2+re3or4+re5, "i");
//unit tests
console.log(reg.test("opacity(0)")); //true
console.log(reg.test("opacity(0.4)")); //true
console.log(reg.test("opacity(1)")); //true
console.log(reg.test("opacity()")); //false
console.log(reg.test("opacity")); //false
What have I screwed up? (It's returning false on most tests where it should be true)
Is there a better way (i.e. a way to access the various constituent parts of the filter string with object notation or something similar)? Parsing strings to fish out properties is painful.