{"record":{"id":"3fe7377099bb4c28","repo":"pentaho/pentaho-kettle","slug":"the-function-call-replace-is-not-valid-wrong-number-of","errorCode":null,"errorMessage":"The function call replace is not valid (wrong number of arguments)","messagePattern":"The function call replace is not valid \\(wrong number of arguments\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1521,"sourceCode":"    }\n  }\n\n  public static String replace( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n    try {\n      if ( ArgList.length >= 2 && ( ArgList.length - 1 ) % 2 == 0 ) {\n        if ( isNull( ArgList, new int[] { 0, 1 } ) ) {\n          return null;\n        } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {\n          return (String) undefinedValue;\n        }\n        String objForReplace = (String) ArgList[0];\n        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {\n          objForReplace = objForReplace.replaceAll( (String) ArgList[i], (String) ArgList[i + 1] );\n        }\n        return objForReplace;\n      } else {\n        throw new RuntimeException( \"The function call replace is not valid (wrong number of arguments)\" );\n      }\n    } catch ( Exception e ) {\n      throw new RuntimeException( \"Function call replace is not valid : \" + e.getMessage() );\n    }\n  }\n\n  // Implementation of the JS AlertBox\n  public static String Alert( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n\n    SpoonInterface spoon = SpoonFactory.getInstance();\n    if ( ArgList.length == 1 && spoon != null ) {\n      String strMessage = (String) ArgList[0];\n      spoon.messageBox( strMessage, \"Alert\", false, Const.INFO );\n    }\n\n    return \"\";\n  }","sourceCodeStart":1503,"sourceCodeEnd":1539,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1503-L1539","documentation":"replace applies a sequence of regex replacement pairs to a string: replace(str, regex1, repl1, regex2, repl2, ...). It requires an odd argument count (the string plus whole regex/replacement pairs); any other count triggers this RuntimeException. Note it also requires more than just the string - pairs are mandatory.","triggerScenarios":"Calling replace(str) with only the string, or with an even number of trailing arguments so a pair is incomplete (e.g. replace(str, 'a') or replace(str, 'a', 'b', 'c')).","commonSituations":"Adding a third argument to an existing call and breaking pair parity, thinking replace(str, regex) works like String.replace, mixing up replace/replaceAll semantics.","solutions":["Ensure arguments are: 1 string + N complete (regex, replacement) pairs - odd total count greater than 1.","Fix pair parity at the call site.","Remember arguments are Java regexes; escape special characters like '.' correctly."],"exampleFix":"// before\nvar s = replace(text, 'a', 'b', 'c'); // incomplete pair -> throws\n// after\nvar s = replace(text, 'a', 'b', 'c', '');","handlingStrategy":"validation","validationCode":"function safeReplace(s) {\n  if (typeof s !== 'string' || (arguments.length - 1) < 2 || (arguments.length - 1) % 2 !== 0) {\n    return s;\n  }\n  return replace.apply(null, arguments);\n}","typeGuard":"function isReplaceCall(args) {\n  return typeof args[0] === 'string' && args.length > 2 && (args.length - 1) % 2 === 0;\n}","tryCatchPattern":"try {\n  var s = replace(text, 'a', 'b');\n} catch (e) {\n  if (String(e.message).indexOf('wrong number of arguments') >= 0) {\n    // fix arity: 1 string + complete regex/replacement pairs\n  }\n}","preventionTips":["Argument count must be odd (string + whole pairs) and greater than 1.","Replace() takes complete (regex, replacement) pairs - never a lone regex.","Test regexes in a validator before embedding them in scripts."],"tags":["java","pdi","scripting","argument-count","regex"],"backgroundTag":"missing-required-argument","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}