Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:12:02+00:00 2026-06-07T13:12:02+00:00

I am writing a sort of instruction string parser for a project so that

  • 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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:12:03+00:00Added an answer on June 7, 2026 at 1:12 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some sort of backup tool that has to copy all the files
I am writing a string compare function to sort medical terms that often contain
I'm writing a Dashboard widget in Dashcode, and I'd like to add some sort
I'm writing code in python for some sort of daemon that has to execute
I am writing a sort of image processing program that allows the user to
Scala programmer should have known that this sort of writing : class Person{ var
Writing htaccess that allows me to remove index.php from the URL can confuse search
Writing documentation in html requires some code examples. What to do with characters that
I'm writing some custom helpers the current one involves a sort of datagrid control
I writing a SP that accepts as parameters column to sort and direction. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.