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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:20:36+00:00 2026-05-19T02:20:36+00:00

The following (Java) implementation is part of the GDK: /** * Replaces all occurrences

  • 0

The following (Java) implementation is part of the GDK:

/**
 * Replaces all occurrences of a captured group by the result of a closure on that text.
 * <p/>
 * <p> For examples,
 * <pre>
 *     assert "FOOBAR-FOOBAR-" == "foobar-FooBar-".replaceAll(~"(([fF][oO]{2})[bB]ar)", { Object[] it -> it[0].toUpperCase() })
 * <p/>
 *     Here,
 *          it[0] is the global string of the matched group
 *          it[1] is the first string in the matched group
 *          it[2] is the second string in the matched group
 * <p/>
 * <p/>
 *     assert "FOO-FOO-" == "foobar-FooBar-".replaceAll("(([fF][oO]{2})[bB]ar)", { x, y, z -> z.toUpperCase() })
 * <p/>
 *     Here,
 *          x is the global string of the matched group
 *          y is the first string in the matched group
 *          z is the second string in the matched group
 * </pre>
 * <p>Note that unlike String.replaceAll(String regex, String replacement), where the replacement string
 * treats '$' and '\' specially (for group substitution), the result of the closure is converted to a string
 * and that value is used literally for the replacement.</p>
 *
 * @param self    a String
 * @param pattern the capturing regex Pattern
 * @param closure the closure to apply on each captured group
 * @return a String with replaced content
 * @since 1.6.8
 * @see java.util.regex.Matcher#quoteReplacement(java.lang.String)
 */
public static String replaceAll(final String self, final Pattern pattern, final Closure closure) {
    final Matcher matcher = pattern.matcher(self);
    if (matcher.find()) {
        final StringBuffer sb = new StringBuffer(self.length() + 16);
        do {
            int count = matcher.groupCount();
            List<String> groups = new ArrayList<String>();
            for (int i = 0; i <= count; i++) {
                groups.add(matcher.group(i));
            }
            final String replacement = InvokerHelper.toString(closure.call(groups.toArray()));
            matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
        } while (matcher.find());
        matcher.appendTail(sb);
        return sb.toString();
    } else {
        return self;
    }
}

Are there any similar implementations out there for Javascript? If not, is it feasible to attempt an implementation oneself; how might one go about it?

  • 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-19T02:20:37+00:00Added an answer on May 19, 2026 at 2:20 am

    I hope I’m understanding your question properly, but reading the comments in your example, the sample use cases can be satisfied by the replace method of the js String object. replace can take a function as its replacement argument:

    // true
    console.log(
        "FOOBAR-FOOBAR-" == "foobar-FooBar-".replace(
            /(([fF][oO]{2})[bB]ar)/g,
            function(  all, capture1, capture2 ) {
                return all.toUpperCase();
            }
        )
    );
    
    // true
    console.log(
        "FOO-FOO-" == "foobar-FooBar-".replace(
            /(([fF][oO]{2})[bB]ar)/g,
            function( all, capture1, capture2 ) {
                return capture2.toUpperCase();
            }
        )
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been asked (as part of homework) to design a Java program that does
I am working on a BlackBerry j2me Java implementation that does not have the
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
Does java's TrustManager implementation ignore if a certificate has expired? I tried the following:
I'm looking for an implementation of java.util.Set with the following features: Should be concurrent
i have the following code, taken partly from a Red Black Tree Java implementation.
Is the following java implementation of the visitor pattern using generics, general enough to
The following Java code is throwing a compiler error: if ( checkGameTitle(currGame) ) ArrayList<String>
I have the following Java 6 code: Query q = em.createNativeQuery( select T.* +
Given the following java enum: public enum AgeRange { A18TO23 { public String toString()

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.