code: public String replace( String input, String replacement ) throws ParameterOutOfBoundsException {
          // Replace \ in the replacement with \\ to escape it for Java replace.
          replacement = REPLACEMENT_BACKSLASH.matcher( replacement ).replaceAll( REPLACEMENT_BACKSLASH_FOR_JAVA );
  
          // Replace the Javascript-ese $& in the replacement with Java-ese $0, but
          // watch out for $$&, which should stay $$&, to be changed to \$& below.
          replacement = REPLACEMENT_DOLLAR_AMPERSAND.matcher( replacement ).replaceAll( REPLACEMENT_DOLLAR_AMPERSAND_FOR_JAVA );
  
          // Test for Javascript-ese $` and $', which we do not support in the pure
          // Java version.
          if( REPLACEMENT_DOLLAR_APOSTROPHE.matcher( replacement ).find() ) {
              throw new ParameterOutOfBoundsException();
          }
  
          // Replace the Javascript-ese $$ in the replacement with Java-ese \$.
          replacement = REPLACEMENT_DOLLAR_DOLLAR.matcher( replacement ).replaceAll( REPLACEMENT_DOLLAR_DOLLAR_FOR_JAVA );
  
          Matcher matcher = pattern.matcher( input );
          return global ? matcher.replaceAll( replacement ) : matcher.replaceFirst( replacement );
      }
docs: Replace the matches in the input with the replacement.
  @param input the input string
  @param replacement the replacement
  @return the resulting string
  @throws ParameterOutOfBoundsException if Java can not replace it like Javascript