pentaho/pentaho-kettle · error · RuntimeException

Function call replace is not valid : " + e.getMessage()

Error message

Function call replace is not valid : " + e.getMessage()

What it means

replace wraps any exception raised while performing its regex replacements in a RuntimeException formatted as 'Function call replace is not valid : <message>'. Because java.util.regex.PatternSyntaxException messages appear here, malformed regexes are the most common root cause.

Solutions

  1. Read the message after the colon - a PatternSyntaxException message points at the bad regex index.
  2. Validate each regex with a try { new java.lang.String(str).matches(...) } or test in a regex tool.
  3. Guard against null inputs before calling replace.

Example fix

// before
var s = replace(text, 'a(', 'b'); // invalid regex -> throws
// after
var s = replace(text, 'a\\(', 'b');
Defensive patterns

Strategy: try-catch

Validate before calling

function safeReplace(s) {
  for (var i = 1; i + 1 < arguments.length; i += 2) {
    try { new java.lang.String('').matches(String(arguments[i])); } // throws on bad regex
    catch (e) { return s; }
  }
  try { return replace.apply(null, arguments); } catch (e) { return s; }
}

Type guard

function areStringArgs(args) {
  for (var i = 0; i < args.length; i++) if (typeof args[i] !== 'string') return false;
  return true;
}

Try / catch

try {
  var s = replace(text, rx, rep);
} catch (e) {
  // PatternSyntaxException details follow 'not valid : ' - inspect and fix the regex
  Logger.log(e.message);
  var s = text;
}

Prevention

When it happens

Trigger: Passing an invalid regex pattern (e.g. unbalanced parenthesis 'a(') or null/non-String arguments to replace() in a JavaScript step.

Common situations: Hand-written regexes with escaping mistakes, passing JavaScript RegExp objects instead of strings, null fields arriving from upstream steps.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/fcd88e4b2abdde06. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:1524

  public static String replace( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {
    try {
      if ( ArgList.length >= 2 && ( ArgList.length - 1 ) % 2 == 0 ) {
        if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
          return null;
        } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
          return (String) undefinedValue;
        }
        String objForReplace = (String) ArgList[0];
        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {
          objForReplace = objForReplace.replaceAll( (String) ArgList[i], (String) ArgList[i + 1] );
        }
        return objForReplace;
      } else {
        throw new RuntimeException( "The function call replace is not valid (wrong number of arguments)" );
      }
    } catch ( Exception e ) {
      throw new RuntimeException( "Function call replace is not valid : " + e.getMessage() );
    }
  }

  // Implementation of the JS AlertBox
  public static String Alert( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
    Object FunctionContext ) {

    SpoonInterface spoon = SpoonFactory.getInstance();
    if ( ArgList.length == 1 && spoon != null ) {
      String strMessage = (String) ArgList[0];
      spoon.messageBox( strMessage, "Alert", false, Const.INFO );
    }

    return "";
  }

  // Setting EnvironmentVar
  public static void setEnvironmentVar( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,

View on GitHub (pinned to f3058517a1)