根据另一个数组中的条目拆分一个数组

科纳·奥布赖恩(Conor O'Brien)

我正在研究一个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任何帮助表示赞赏!

m69``干躁不受欢迎''

这会将正则表达式拆分为多个部分,并在第二个数组中填充这些部分的描述。它会跳过意外的字符,但是不会进行真正的正则表达式语法检查,即,如果您开始一个范围而不结束它,则脚本不会抱怨。我可以随意添加列表中缺少的一些内容,例如对括号进行分组,起始和结束锚点...

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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据另一个数组中的数据过滤一个数组

来自分类Dev

根据python中另一个数组的条目计算数组中的新条目

来自分类Dev

根据另一个数组的值对一个数组进行排序?

来自分类Dev

根据另一个数组列表检查一个数组列表

来自分类Dev

根据另一个数组的顺序对一个数组进行排序

来自分类Dev

Mongodb根据另一个数组元素过滤一个数组

来自分类Dev

根据另一个数组中的值替换一个数组中的值

来自分类Dev

根据Python中另一个数组中的索引创建一个数组

来自分类Dev

根据另一个数组中的数据对一个数组进行排序

来自分类Dev

Google表格公式根据另一个数组中的匹配项查询一个数组

来自分类Dev

根据另一个数组中的true或false集映射一个数组

来自分类Dev

根据Google Apps脚本中的另一个数组过滤一个数组

来自分类Dev

如何根据Java中另一个数组的值对一个数组进行排序?

来自分类Dev

在另一个数组中查找一个数组的匹配索引

来自分类Dev

从另一个数组中删除一个数组的内容

来自分类Dev

从PHP中的另一个数组中提取一个数组

来自分类Dev

如何从php中的另一个数组排序一个数组?

来自分类Dev

修改另一个数组中的一个数组

来自分类Dev

从另一个数组中删除一个数组的内容

来自分类Dev

通过另一个数组中的值过滤一个数组?

来自分类Dev

修改另一个数组中的一个数组

来自分类Dev

AngularJs,在一个数组中搜索另一个数组

来自分类Dev

Angular 6:从另一个数组中获取一个数组

来自分类Dev

如何在另一个数组中随机插入数组的条目反应本机

来自分类Dev

根据另一个数组中的顺序对数组数组进行排序

来自分类Dev

在Python中在另一个数组内拆分数组

来自分类Dev

一个数组另一个

来自分类Dev

打印一个数组中的 JSON 值,这个数组在另一个数组中

来自分类Dev

根据另一个数组中的索引获取数组中的元素

Related 相关文章

  1. 1

    根据另一个数组中的数据过滤一个数组

  2. 2

    根据python中另一个数组的条目计算数组中的新条目

  3. 3

    根据另一个数组的值对一个数组进行排序?

  4. 4

    根据另一个数组列表检查一个数组列表

  5. 5

    根据另一个数组的顺序对一个数组进行排序

  6. 6

    Mongodb根据另一个数组元素过滤一个数组

  7. 7

    根据另一个数组中的值替换一个数组中的值

  8. 8

    根据Python中另一个数组中的索引创建一个数组

  9. 9

    根据另一个数组中的数据对一个数组进行排序

  10. 10

    Google表格公式根据另一个数组中的匹配项查询一个数组

  11. 11

    根据另一个数组中的true或false集映射一个数组

  12. 12

    根据Google Apps脚本中的另一个数组过滤一个数组

  13. 13

    如何根据Java中另一个数组的值对一个数组进行排序?

  14. 14

    在另一个数组中查找一个数组的匹配索引

  15. 15

    从另一个数组中删除一个数组的内容

  16. 16

    从PHP中的另一个数组中提取一个数组

  17. 17

    如何从php中的另一个数组排序一个数组?

  18. 18

    修改另一个数组中的一个数组

  19. 19

    从另一个数组中删除一个数组的内容

  20. 20

    通过另一个数组中的值过滤一个数组?

  21. 21

    修改另一个数组中的一个数组

  22. 22

    AngularJs,在一个数组中搜索另一个数组

  23. 23

    Angular 6:从另一个数组中获取一个数组

  24. 24

    如何在另一个数组中随机插入数组的条目反应本机

  25. 25

    根据另一个数组中的顺序对数组数组进行排序

  26. 26

    在Python中在另一个数组内拆分数组

  27. 27

    一个数组另一个

  28. 28

    打印一个数组中的 JSON 值,这个数组在另一个数组中

  29. 29

    根据另一个数组中的索引获取数组中的元素

热门标签

归档