1
\$\begingroup\$

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;
}
\$\endgroup\$
2
\$\begingroup\$

The else if block was pretty hard to follow. I would say that your function has rather weird behaviour (treating the text as flags) if the input has a leading slash but no trailing slash.

Anyway, even if you are not very familiar with JavaScript, surely you are familiar with regular expressions? Why not just use a regular expression to parse your regular expression?

function regGen(uInput) {
    var regExpParser = new RegExp("^/(.*)/(.*)|(.*)");
    var match = uInput.match(regExpParser);
    return new RegExp(match[1] || match[3], match[2]);
}

Strictly speaking, we should both be more careful about interpreting possible backslashes in the input, but that is rather tricky to do correctly.

I've taken out the alert(); a malformed expression would probably be better handled by the caller.

\$\endgroup\$
3
  • \$\begingroup\$ I actually didn't know that the javascript string.match would return matches, that's the aspect I was missing (well that and lack of negative lookbehind)). As far as the alert part, yours will cause a JS error if malformed input is passed, so I'd still want to validate first. \$\endgroup\$ – Trel Feb 29 '16 at 20:56
  • \$\begingroup\$ I made some modifications to suit my needs a bit, does this still appear to be sane? I tried to accommodate for bad user input. In all cases, I want to return a valid regex, but in the case of a bad user input that can't possibly generate such, I want to return a regex that will match nothing. Pastebin of modifications: pastebin.com/hZKjhihU \$\endgroup\$ – Trel Feb 29 '16 at 22:09
  • \$\begingroup\$ In that case, I would write function regGen(uInput) { try { …; return …; } catch (err) { return /(?!)/; } }. In other words, if anything goes wrong, return the default. \$\endgroup\$ – 200_success Feb 29 '16 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.