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

  • Home
  • SEARCH
  • 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 7810359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:44:54+00:00 2026-06-02T03:44:54+00:00

Background I am currently writing a function which needs to accept a command as

  • 0

Background

I am currently writing a function which needs to accept a command as a string. The string needs to be split into it’s finer parts so that the function may then use those parts. Here is an example string:

People.filter(Hometown.eq("Pittsburgh"))

Which, in this simplified example, I would need to resolve to something like:

var objectToTraverse = "People";
var actionToTake = "filter";
var comparer = "Hometown";
var targetValue = "Pittsburgh";

For the example above, the split operation is fairly easy:

var _command = "People.filter(Hometown.eq("Pittsburgh"))";
var command = _command.split(".");

var objectToTraverse = command[0];
var actionToTake = command[1].split("(")[0];
var comparer = command[1].split("(")[1];
var targetValue = command[2].split("(")[1].split(")")[0];

But what if you have a more complex command such as this:
People.filter(Hometown.eq("Pittsburgh") && Gender.eq("male") || Age.eq(>= 35)).orderBy(Age,Gender).limitResultSet(10)

The pattern may be different each time.

Question

Using JavaScript, how do I efficiently parse a string-command based on various mixed delimiters and conditions such as this:

People.filter(Hometown.eq("Pittsburgh") && Gender.eq("male") || Age.eq(>= 35)).orderBy(Age,Gender).limitResultSet(10)

Please Note

I am aware of all of the frameworks available already for querying data in JavaScript and, while I appreciate all tips and advice, I would still like an answer to this question regardless of what else is available out there.

  • 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-02T03:44:56+00:00Added an answer on June 2, 2026 at 3:44 am

    Regex, particularly JavaScript’s flavor of regex which lacks recursion and balanced groups, is not up to the task of parsing code like this. Your sample code, with its well-formed nested parentheses, is a classic example of a context-free grammar, which far wiser men than I have declared undoable with regex.

    What you need is an actual parser. There are full JavaScript parsers available that are, themselves, implemented in JavaScript (JSLint has one in it). Or, if you only want to allow certain types of expressions, you can generate a parser to suit any grammar you choose.

    One thing to be aware of: This is definitely doable with a parser (albeit not with regex alone). But it’s unlikely to be easy or simple. If this is for a real-world project, unless you have a driving reason to write your own markup/programming language, I suggest you consider using an existing solution. If you’re describing expressions rather than imperative code (e.g. LINQ vs. classic C#), XML or JSON could be good ways to do it.

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

Sidebar

Related Questions

Background : I'm currently writing a greasemonkey script that embeds/modifies a specific page's html.
Quick background: writing a module. One of my objects has methods that may or
I am working on writing a Bash function to start a server that needs
Background: I'm writing a Java server application that needs to perform basic interactions with
I'm writing a JavaScript function that needs to uphold three properties: be very small
I'm currently writing a programme which has a function to hash a number of
Background Currently, I am working on a Rails application. I have different products that
I'm currently building a simple application on Gjs , which should change the background-image
I am currently writing my first Android application and I keep running into references
I'm currently writing an application that allows one to store images, and then tag

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.