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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:12:05+00:00 2026-05-13T20:12:05+00:00

I have a whole array of regexp queries to make recursively going through the

  • 0

I have a whole array of regexp queries to make recursively going through the string in order.

Is there a way I can do it all with 1 call such as (doesn’t work)?

str.replace(/query1|query2|query3|query4|...|[0-9]+|[^]/,
             "reslt1|reslt2|reslt3|reslt4|...|procNumb|procChar");

It need only work in Firefox. Right now I’m stuck with:

str.replace(... ,function(a,b,c){if (a=="query1") return "reslt1"; ..............});

Thanks!

Edit: Sorry if confusing. Goal:

Before: “query1query3x123query2”
After: “reslt1reslt3procChar(x)procNumb(123)reslt2”

The main thing is I need I to process the string 1 fragment at a time recursively, so I think I must use a super-query like that to match any, or not use regexes at all. I just wonder if there’s a way to automatically pair the results to the queries when using lots of pipes. I am not so familiar with javascript regex but I couldn’t find anything on mdc unfortunately.

  • 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-13T20:12:05+00:00Added an answer on May 13, 2026 at 8:12 pm

    If you’re trying to match any of several alternative substrings and provide a different result for each match, you’re going to have to use a function (as you appear to be doing). For instance:

    var str = "ABCDE";
    
    str = str.replace(/A|B|C|D/g, function(match) {
        var rv;
        switch (match)
        {
            case 'A':
                rv = '1';
                break;
            case 'B':
                rv = '2';
                break;
            case 'C':
                rv = '3';
                break;
            case 'D':
                rv = '4';
                break;
        }
        return rv;
    });
    alert(str); // Alerts 1234E
    

    (Of course, that particular example would be better done as an array lookup.)

    If you’re a vertical brevity fiend (I’m not, but some are), you can use early returns:

    str = str.replace(/A|B|C|D/g, function(match) {
        switch (match)
        {
            case 'A': return '1';
            case 'B': return '2';
            case 'C': return '3';
            case 'D': return '4';
        }
    });
    alert(str); // Alerts 1234E
    

    Edit Just to be clear, about my “array lookup” comment above: If the replacements really are just a static lookup, you can do this:

    var str, replacements, key, rex;
    
    // Potentially one-time prep, if you're re-using this lookup
    replacements = {
        "A": "1",
        "B": "2",
        "C": "3",
        "D": "4"
    };
    rex = [];
    for (key in replacements)
    {
        rex.push(key);
    }
    rex = new RegExp(rex.join('|'), "g");
    
    // The bit you reuse
    str = "ABCDE";
    str = str.replace(rex, function(match) {
        return replacements[match];
    });
    alert(str); // Alerts 1234E
    

    …since JavaScript objects are associative arrays.

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

Sidebar

Related Questions

I have a whole bunch of buttons that all need to have both a
If I have a string of HTML, maybe like this... <h2>Header</h2><p>all the <span class=bright>content</span>
I have the below code snippet: line_sub = Regexp.new(/\s+||\[|\]/) tmp = Array.new # reading
I have an array of strings in python which each string in the array
I have an large javascript array, 5000 or so entries. In order to run
I have a problem initializing an array whose size is defined as extern const.
I have an object whose value may be one of several array types like
While working with ActiveRecord I have a table which stores a serialized array of
I have whole bunch of <div class=productlistname><a href=#>The Text!</a></div> . What I want to
I have a URL list of jar files, also I have whole path classname

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.