--- type: "fhirpath-function" title: "FHIRPath Function: replace" function: "replace" category: "String Manipulation" section: "5.6.9" source: "fhirpath/functions.json" --- # FHIRPath Function: replace Returns the input string with all instances of `pattern` replaced with `substitution`. If the substitution is the empty string (`''`), instances of `pattern` are removed from the result. If `pattern` is the empty string (`''`), every character in the input string is surrounded by the substitution, e.g. `'abc'.replace('','x')`{:.fhirpath} becomes `'xaxbxcx'`. If the input collection, `pattern`, or `substitution` are empty, the result is empty (`{ }`). If the input collection contains multiple items, the evaluation of the expression will end and signal an error to the calling environment. ``` fhirpath 'abcdefg'.replace('cde', '123') // 'ab123fg' 'abcdefg'.replace('cde', '') // 'abfg' 'abc'.replace('', 'x') // 'xaxbxcx' ``` ## Summary - **Category**: String Manipulation - **Section**: `5.6.9` - **Return Type**: `String` - **Empty Input Result**: `empty` - **Errors on Multiple Input**: `true` ## Description Returns the input string with all instances of `pattern` replaced with `substitution`. If the substitution is the empty string (`''`), instances of `pattern` are removed from the result. ## Arguments - `pattern`: `String` - Pattern to replace - `substitution`: `String` - Substitution for the pattern ## Type Mapping - `String-String` ## Example ```fhirpath 'abcdefg'.replace('cde', '123') // 'ab123fg' 'abcdefg'.replace('cde', '') // 'abfg' 'abc'.replace('', 'x') // 'xaxbxcx' ```