{"record":{"id":"fcd88e4b2abdde06","repo":"pentaho/pentaho-kettle","slug":"function-call-replace-is-not-valid-e-getmessage","errorCode":null,"errorMessage":"Function call replace is not valid : \" + e.getMessage()","messagePattern":"Function call replace is not valid : \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1524,"sourceCode":"  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  }\n\n  // Setting EnvironmentVar\n  public static void setEnvironmentVar( ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,","sourceCodeStart":1506,"sourceCodeEnd":1542,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1506-L1542","documentation":"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.","triggerScenarios":"Passing an invalid regex pattern (e.g. unbalanced parenthesis 'a(') or null/non-String arguments to replace() in a JavaScript step.","commonSituations":"Hand-written regexes with escaping mistakes, passing JavaScript RegExp objects instead of strings, null fields arriving from upstream steps.","solutions":["Read the message after the colon - a PatternSyntaxException message points at the bad regex index.","Validate each regex with a try { new java.lang.String(str).matches(...) } or test in a regex tool.","Guard against null inputs before calling replace."],"exampleFix":"// before\nvar s = replace(text, 'a(', 'b'); // invalid regex -> throws\n// after\nvar s = replace(text, 'a\\\\(', 'b');","handlingStrategy":"try-catch","validationCode":"function safeReplace(s) {\n  for (var i = 1; i + 1 < arguments.length; i += 2) {\n    try { new java.lang.String('').matches(String(arguments[i])); } // throws on bad regex\n    catch (e) { return s; }\n  }\n  try { return replace.apply(null, arguments); } catch (e) { return s; }\n}","typeGuard":"function areStringArgs(args) {\n  for (var i = 0; i < args.length; i++) if (typeof args[i] !== 'string') return false;\n  return true;\n}","tryCatchPattern":"try {\n  var s = replace(text, rx, rep);\n} catch (e) {\n  // PatternSyntaxException details follow 'not valid : ' - inspect and fix the regex\n  Logger.log(e.message);\n  var s = text;\n}","preventionTips":["Escape regex metacharacters in patterns and replacements (esp. '$' in replacements).","Pass plain strings, not RegExp objects.","Null-check all inputs before calling replace."],"tags":["java","pdi","scripting","regex","wrapped-exception"],"backgroundTag":"invalid-regex-pattern","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"}