replace¶
The replace filter is designed to replace all occurrences of a specific substring with another string. It is a versatile tool for modifying and transforming text content.
Functionality
- Strings: Takes a string as input.
- Search and Replace: Requires two string arguments:
old_string: The substring to search for within the input string.new_string: The string to replace all occurrences ofold_stringwith.
- All Occurrences: Unlike
replace_first, this filter replaces every instance of the old substring. - Output: Returns a new string with all replacements made.
Syntax
Arguments- old_string: The substring you want to find and replace.
- new_string: The string that will be substituted in place of the
old_string.
Code Samples
Example 1: Replacing All Occurrences of a Word
Output: Example 2: Replacing a Single Character Output: Example 3: Replacing with an Empty StringOutput:
Outliers and Special Cases¶
- Empty Strings:
- If
old_stringis empty, the filter does nothing and returns the original string. - If
new_stringis empty, all occurrences ofold_stringare effectively removed.
- If
- Substring Not Found: If
old_stringis not found within theinput_string, the original string is returned unchanged. - Non-String Input: If the input or arguments are not strings, the filter might attempt to convert them to strings or return an error, depending on how Experience Builder handles type conversions.
Key Points¶
- The
replacefilter is a powerful string manipulation tool, allowing for bulk replacements within text. - It is case-sensitive. "The" and "the" would be considered distinct and replaced separately.
- Be aware of the potential impact of replacing with an empty string, as it effectively deletes all occurrences of the
old_string. - You can chain this filter with other string filters to perform more complex text transformations.