0

I am writing a sort of instruction string parser for a project so that users can write "instructions" to do things.

So some example "Instructions"

ADD 5 TO 3
FLY TO MOON
GOTO 10 AND MOVE 50 PIXELS

I assign these to an array of strings

var Instructions = ["ADD * TO *","FLY TO *", "GOTO * AND MOVE * PIXELS"];

If I have some:

var input = // String

And that string could be something like ADD 5 to 8 or FLY TO EARTH

Is there a regexp search of match I could use to help me find which instruction matched? For example

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
}
4
  • You would need to give all possible commands and operators before anyone can give a "regex" which would actually just turn into a parser. Commented Jul 14, 2012 at 17:33
  • @ShoMinamimoto I was hoping that there's be some wild card character that could replace the X and Ys. Commented Jul 14, 2012 at 17:34
  • Well then you need to define a grammar and possible inputs. Are X and Y integers? Characters? Words with spaces? Can commands have integers, symbols, etc? (like is there a command that is called MOVE52). Define what exactly you want to capture and what you don't. Commented Jul 14, 2012 at 17:42
  • @ShoMinamimoto Ideally just strings (with no spaces if that makes it easier) Commented Jul 14, 2012 at 17:43

1 Answer 1

1

You could do something like this.

//setup
var instruction_patterns = [/ADD (\w+) TO (\w+)/, /FLY TO (\w+)/],
    input = "ADD 4 TO 3",
    matches;

//see if any instructions match and capture details
for (var i=0, len=instruction_patterns.length; i<len; i++)
    if (matches = input.match(instruction_patterns[i]))
        break;

//report back
if (matches)
    alert(
        '- instruction:\n'+matches[0]+'\n\n'+
        '- tokens:\n'+matches.slice(1).join('\n')
    );

Note the patterns are stored as REGEXP literals. Also note that, despite the comment in your original code, matches[0] will always be the entire match, so this cannot be the first token (4). That will be in matches[1].

I have assumed in the patterns that the tokens could be anything alphanumeric (\w), not necessarily numbers. Adjust this as required.

Finally, to allow case-insensitive, just add the i flag after each pattern (/pattern/i).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.