My goal is to accept user input regex in the following forms
/regex|here/i
/regex|here/
regex|here
From there I want to be able to pass that to a function and return a RexExp object for use later with string.match()
I came up with the below to achieve that, and not break the caller which is expecting the RegExp object returned. Is there any part of this that I'm doing wrong or exceedingly sub-optimally? (I'm not very familiar with Javascript).
function regGen(uInput) { var inReg = ""; var inMod = ""; var outReg = /(?!)/ if(uInput[0] == "/" && uInput[uInput.length-1] == "/") { inReg = uInput.slice(1,uInput.length-1); } else if(uInput[0] == "/") { var temp = uInput.split("/"); inMod = temp[temp.length-1]; inReg = temp; inReg.pop(); inReg.shift(); inReg = inReg.join("/"); } else { inReg = uInput; } try { outReg = new RegExp(inReg,inMod); } catch(err) { alert("Malformed Regex"); } return outReg; }