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 7586071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:20:02+00:00 2026-05-30T19:20:02+00:00

I am creating a recursive js function which basically parses a string. I do

  • 0

I am creating a recursive js function which basically parses a string. I do not get to choose the syntax of the string, so i can’t change that. its delimited by a “, ” which works fine, expect when they are in parenthesis, it needs to ignore inside of the parentheses, in this case it needs to split after the parenthesis. Here is an example.

String: "h5, h7, (h5, h7, r3)7, h9"

And it needs to be transformed into an array of strings that looks like

array(
"h5",
"h7",
"(h5, h7, r3)7", 
"h9"
);

I know there is probably a way to do it with regex, and I have a very basic regex knowledge, but I can’t figure it out. If it helps here is the js code I have.

cols = pattern.split(',');// This is where the regex would go.
                $.each(cols, function(index, val){
                val = $.trim(val);
                    var type = "";
                    var ctr = 0;
                    for(ctr = 0; ctr < val.length && isAlpha(val[ctr]); ctr++)
                        type += val[ctr];
                    if(val[0] == "("){
                        open = 1;
                        substr = "";
                        i = 1;
                        for(; i < val.length && open > 0; i++ ){
                            if(val[i] == ")"){
                                open--;
                            }
                            else
                                substr += val[i];
                        }
                    var repeater = val.substr(i);
                    if(isNumeric(repeater)){
                        for(j=0; j < repeater; j++){
                          colLen += updateRow(row, substr, false);
                        }
                    }
                    else{
                        $('#'+row).append('<a href="" >SE</a>');
                        colLen++;
                    }
                }
               // If it doesn't start with a parenthesis, do print it as usual

Obviously there is a lot more code, but that is the relevant part.

Sorry If i formatted it wrong, this is my first question.

  • 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-05-30T19:20:04+00:00Added an answer on May 30, 2026 at 7:20 pm

    Here are a couple brute force methods that work:

    Method 1: Match the parenthesized portions and temporarily replace the commas inside with something else so you can then split on comma and then put the commas back in the parenthesized pieces.

    var data = "h5, h7, (h5, h7, r3)7, h9";
    
    var temp = data.replace(/\([^)]+\)/, function(match) {
        return(match.replace(/,/g, "xx!xx"));
    });
    
    var splitData = temp.split(/\s*,\s*/);
    for (var i = 0; i < splitData.length; i++) {
        splitData[i] = splitData[i].replace(/xx!xx/g, ",");
    }
    
    // splitData = ["h5", "h7", "(h5, h7, r3)7", "h9"]
    

    Method 2: Split the whole string by a comma and then puts the parenthesized pieces back together again:

    var data = "h5, h7, (h5, h7, r3)7, h9";
    
    var splitData = data.split(",");
    var output = [], pieces, inParen = false;
    for (var i = 0; i < splitData.length; i++) {
        temp = splitData[i];
        if (inParen) {
            pieces += "," + temp;
            if (temp.indexOf(")") != -1) {
                inParen = false;
                output.push(pieces);
            }
        } else {
            temp = $.trim(temp);
            if (temp.indexOf("(") != -1) {
                pieces = temp;
                inParen = true;
            } else {
                output.push(temp);
            }
        }
    }
    
    // output = ["h5", "h7", "(h5, h7, r3)7", "h9"]
    

    You can see both of these work here: http://jsfiddle.net/jfriend00/cs224/

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

Sidebar

Related Questions

Need some help in creating function which can create folders recursively with giving path:
jQuery's extend function has the option of recursive extension, which I use when creating
Does anyone know the logic behind creating a recursive function to generate all combinations
For a homework project, I'm creating a PHP driven website which main function is
The function below basically emulates mkdir -p , recursively creating directories for a given
I've been creating a function that acts like a paint bucket tool for the
I am creating a TFS tool that will get changeset information from the TFS
Creating liquid layouts is an immense pain. Now, I totally understand that tables should
Creating a simple RPG game, first time using XNA. Trying to get my character
We have a fairly simple program that's used for creating backups. I'm attempting to

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.