私は、ユーザーが物事を行うための「指示」を書くことができるように、プロジェクトの一種の指示文字列パーサーを書いています。
だからいくつかの例「指示」
ADD 5 TO 3
FLY TO MOON
GOTO 10 AND MOVE 50 PIXELS
これらを文字列の配列に割り当てます
var Instructions = ["ADD * TO *","FLY TO *", "GOTO * AND MOVE * PIXELS"];
私がいくつか持っている場合:
var input = // String
そして、その文字列は次のようなものになる可能性がありADD 5 to 8
ますFLY TO EARTH
どの命令が一致したかを見つけるのに役立つ一致の正規表現検索はありますか?例えば
var numInstructions = Instructions.length;
for (var j = 0; j < numInstructions; j++)
{
var checkingInstruction = Instructions[j];
// Some check here with regexp to check if there is a match between checkingInstruction and input
// Something like...
var matches = input.match(checkingInstruction);
// ideally matches[0] would be the value at the first *, matches[1] would be the value of second *, and checkingInstruction is the instruction that passed
}