根据另一个数组中的条目拆分数组
问题描述:
我正在研究一个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
。任何帮助表示赞赏!
答
这将部分拆分正则表达式,并用部分描述填充第二个数组。它会跳过意想不到的字符,但是没有真正的正则表达式语法检查,即如果您启动了一个范围并且没有结束它,那么脚本就不会发出抱怨。我加入了一些事情从你的列表中缺少的,像分组括号的自由,开始和结束锚...
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); \t // 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);
有一点需要指出的是,在你的'classes'数组,像这样的字符串\“\ w”'将变成只是'“w”'。如果你想保留字符串中的反斜杠,那么你需要'“\\ w”'。 – jfriend00
@ jfriend00啊谢谢! –