根据另一个数组

时间:2015-06-19 20:13:26

标签: javascript arrays regex split

我正在开发一个JavaScript代码,它将RegExp分解为其基本组件,并对其作用做一个小解释。

我的一般想法是将输入字符串(作为RegExp)拆分为另一个数组中的条目。

我目前的代码:

function interpret(regex){
    var r = regex + "";
    r = r.split("/");
    body = r[1];
    flags = r[2];
    var classes     = [".","\w","\d","\s","\W","\D","\S","[","]"];
    var classdefs   = ["any non-newline character","any word (digit or letter)","any digit (characters 0-9)","any whitespace character","any non-word (non-digit and non-letter)","any non-digit (not characters 0-9)","open matchset","close matchset"];
    var quantifiers = ["*","+","?",
                        /{(\d+)}/g,         // a{n} 
                        /{(\d+),}/g,        // a{n,}
                        /{(\d+),(\d+)}/g,   // a{n,m}
                        /[+*?]\?/g          // a<quant>? - lazy quantification
                      ];
    var quantDefs   = ["repeated 0 or more times","repeated 1 or more times","repeated once or not at all","repeated exactly $1 time","repeated $1 or more times","repeated between $1 and $2 times"];
    var escaped     = ["\t","\n","\r","\.","\*","\\","\^","\?","\|"];
    var escapedDefs = ["a tab","a linefeed","a carriage return","a period","an asterisk","a backslash","a carot","a question mark","a vertical bar"];

    // code to split r based on entries in classes, quantifiers, and escaped.
}

理想情况下,此函数(让我们称之为splitR)将返回如下输出:

> splitR("hello",["he","l"]);
["he", "l", "l", "o"]
> splitR("hello",["he"]);
["he", "llo"]
> splitR("hello",["he","o"]);
["he", "ll", "o"];
> splitR("5 is the square root of 25",[/\d+/g,/\w{3,}/g,"of"]);
["5", " is ", "the", " ", "square", " ", "root", " ", "of", " ", "25"]

明确定义,splitR函数应在interpret函数的上下文中使用RegExp并将其拆分为其基本组件;例如\d+[0-9]\w*?应该分为["\d", "+", "[", "0-9", "]", "\w", "*", "?"]。这些组件在其他阵列中单独定义,使用各种RegExps(例如/{(\d+)}/g查找a{n})和字符串(例如".")。

真的,我对splitR的定义感到困惑。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

这将在部分中拆分正则表达式,并使用部件描述填充第二个数组。它会跳过意外的字符,但是没有真正的正则表达式语法检查,即如果你开始一个范围并且没有结束它,脚本就不会抱怨。我冒昧地添加了一些你的列表中缺少的东西,比如分组括号,开始和结束锚......

&#13;
&#13;
function interpret(regex)
{
    var body = regex.source;
    var flags = (regex.global ? "g" : "") + (regex.ignoreCase ? "i" : "") + (regex.multiline ? "m" : "");
    var classes     = [/^\w\-\w/,/^\./,/^\\w/,/^\\d/,/^\\s/,/^\\W/,/^\\D/,/^\\S/,/^\[/,/^\]/,/^\(/,/^\)/,/^\^/,/^\$/,/^\|/];
    var classDefs   = ["character range","any non-newline character","any word (digit or letter)","any digit (characters 0-9)","any whitespace character","any non-word (non-digit and non-letter)","any non-digit (not characters 0-9)","any non-whitespace character","open matchset","close matchset","open group","close group","start anchor or negation","end anchor","alternative"];
    var quantifiers = [/^[+*?]\?/,/^\*/,/^\+/,/^\?/,/^{(\d+)}/,/^{(\d+),}/,/^{(\d+),(\d+)}/];
    var quantDefs   = ["lazy quantification","repeated 0 or more times","repeated 1 or more times","repeated once or not at all","repeated exactly $1 time","repeated $1 or more times","repeated between $1 and $2 times"];
    var escaped     = [/^\\t/,/^\\n/,/^\\r/,/^\\\./,/^\\\*/,/^\\\+/,/^\\\-/,/^\\\\/,/^\\\^/,/^\\\$/,/^\\\?/,/^\\\|/,/^\\\[/,/^\\\]/,/^\\\(/,/^\\\)/,/^\\\{/,/^\\\}/];
    var escapedDefs = ["a tab","a linefeed","a carriage return","a period","an asterisk","a plus","a minus","a backslash","a caret","a dollar sign","a question mark","a vertical bar","a square bracket","a square bracket","a bracket","a bracket","a curly bracket","a curly bracket"];
    var literal     = [/^[^\.\\\[\]\(\)\^\$\|\*\+\-\?\{\}]+/];
    var literalDefs = ["literal text"];
    var regs = classes.concat(quantifiers,escaped,literal);
    var defs = classDefs.concat(quantDefs,escapedDefs,literalDefs);
    var reglist = [];
    var deflist = [];

    while (body.length)
    {
        var found = false;
        var chunk = null;

        for (var i = 0; i < regs.length; i++)
        {
            chunk = body.match(regs[i]);

            if (chunk)
            {
                reglist.push(chunk[0]);
                deflist.push(defs[i]);
                body = body.substr(chunk[0].length);
                found = true;
                break;
            }
        }

        if (!found)
        {
            body = body.substr(1);	// skip unexpected character
        }
    }

    console.log(regex.source);
    console.log(reglist);
    console.log(deflist);
    alert("see console for output");
}

var x = new RegExp("^[a-z0-9]\\^.\\.\\w\\d\\s\\W\\D\\S+(te|\\|st)*\\*\\n+\\+\\-\\}(\\W?\\?\\s{1,3})\\\\*?a{3}b{4,}c{}\\r\\t\\$$", "ig");
interpret(x);
&#13;
&#13;
&#13;