{"record":{"id":"1956ce3e5480534c","repo":"pentaho/pentaho-kettle","slug":"function-call-replace-is-not-valid","errorCode":null,"errorMessage":"Function call replace is not valid : ","messagePattern":"Function call replace is not valid : ","errorType":"exception","errorClass":"RhinoRuntimeError","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1560,"sourceCode":"    Function 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) Context.getUndefinedValue();\n        }\n        String objForReplace = Context.toString( ArgList[0] );\n        for ( int i = 1; i < ArgList.length - 1; i = i + 2 ) {\n          objForReplace =\n            objForReplace.replaceAll( Context.toString( ArgList[i] ), Context.toString( ArgList[i + 1] ) );\n        }\n        return objForReplace;\n      } else {\n        throw Context.reportRuntimeError( \"The function call replace is not valid (wrong number of arguments)\" );\n      }\n    } catch ( Exception e ) {\n      throw Context.reportRuntimeError( \"Function call replace is not valid : \" + e.getMessage() );\n    }\n  }\n\n  // Implementation of the JS AlertBox\n  public static String Alert( Context actualContext, Scriptable actualObject, Object[] ArgList,\n    Function FunctionContext ) {\n\n    SpoonInterface spoon = SpoonFactory.getInstance();\n    if ( ArgList.length == 1 && spoon != null ) {\n      String strMessage = Context.toString( ArgList[0] );\n      boolean ok = spoon.messageBox( strMessage, \"Alert\", true, Const.INFO );\n      if ( !ok ) {\n        throw new RuntimeException( \"Alert dialog cancelled by user.\" );\n      }\n    }\n\n    return \"\";\n  }","sourceCodeStart":1542,"sourceCodeEnd":1578,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1542-L1578","documentation":"The ScriptValuesMod `replace()` function wraps itsreplaceAll loop in try/catch and re-throws any exception as this runtime error with the underlying message appended. Here the appended message was empty, indicating an exception with null getMessage(), commonly a null source string or a bad regex pattern producing an opaque error.","triggerScenarios":"Null or non-string first argument passed to replaceAll; a search argument containing an invalid regex (replaceAll takes regex); Context.toString failing on a non-convertible object.","commonSituations":"Null field values flowing into the JavaScript step on the first row; special regex characters (?, [, +) in search strings that weren't intended as regex; passing numbers instead of strings.","solutions":["Null-check the source string before calling replace, or use a default: (v == null ? '' : v)","Escape regex metacharacters in the search argument, or accept that search is a regex and fix the pattern","Convert arguments to strings explicitly before passing them to replace()"],"exampleFix":"// before\nvar out = replace(rawField, '(?', 'x');\n// after\nvar safe = (rawField == null) ? '' : rawField;\nvar out = replace(safe, '\\\\(\\\\?', 'x');","handlingStrategy":"type-guard","validationCode":"if (str == null) { str = ''; }\ntry { new RegExp(search); } catch (e) { throw new Error('invalid regex in replace: ' + search); }","typeGuard":"function isReplaceArgSafe(s) { return typeof s === 'string'; }","tryCatchPattern":"try {\n  var out = replace(String(v), search, repl);\n} catch (e) {\n  if (String(e).indexOf('replace is not valid') !== -1) { out = String(v); } else { throw e; }\n}","preventionTips":["Escape regex metacharacters in search strings (replaceAll uses regex)","Coerce all inputs with String() before calling replace","Handle null fields explicitly before string manipulation"],"tags":["javascript","pentaho","scripting","regex","null-value"],"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"}