Skip to content

remove

The remove filter is a simple yet powerful tool that allows you to remove all occurrences of a specified substring from a string. This filter can be very useful for cleaning up or manipulating text within your templates.

Overview

The remove filter is used to return a new string that is a copy of the original string but with all occurrences of the specified substring deleted. This makes it straightforward to eliminate unwanted text from dynamic content.

Syntax

Code snippet:

{{ input_string | remove: substring }}

Parameters:

  • input_string: The string from which you want to remove a part.
  • substring: The substring that you want to remove from the input_string.

Examples

Basic Examples

Example 1: Removing a Word

Input:

{{ "Hello, World!" | remove: "World" }}

Output:

Hello, !

Example 2: Removing Special Characters

Input:

{{ "123-456-7890" | remove: "-" }}

Output:

1234567890

Advanced Examples

Example 3: Removing Multiple Occurrences

If the substring appears multiple times in the input string, all occurrences will be removed.

Input:

{{ "Hello, Hello, Hello!" | remove: "Hello" }}

Output:

, , !

Example 4: Removing Whitespaces

Input:

{{ "leading and trailing spaces" | remove: " " }}

Output:

leadingandtrailingspaces

Use Cases

Data Sanitization

Often, JSON keys or other content may contain unwanted characters like hyphens or underscores. You can use the remove filter to sanitize those strings efficiently.

{%- assign sanitized_key = "user_name" | remove: "_" -%}

Cleaning Up URLs

While manipulating URLs, sometimes it's necessary to remove unwanted query parameters or fragments:

{%- assign clean_url = "https://example.com/view?reload=true" | remove: "?reload=true" -%}

Edge Cases and Error Handling

  • Substring Not Found: If the substring to be removed is not found in the input string, the filter will return the original input string unchanged.
  • Empty Substring: If the substring provided is an empty string, the filter will return the original input string.
{{ "example string" | remove: "" }} {# "example string" #}

Additional Tips

  • Ensure that the substring to be removed is correctly defined, especially when dealing with special characters.
  • The remove filter is not case-sensitive, so "REMOVE" will be treated differently from "remove".

Conclusion

The remove filter is a versatile tool in your Experience Builder liquid arsenal. It provides a convenient way to clean up and manipulate strings to fit your presentation requirements. Whether it's for removing specific words, characters, or cleaning up data for further processing, this filter offers a straightforward solution.