def _escape_js_string(string: str) -> str:
    """Escape the string for use as a JS string literal.

    Args:
        string: The string to escape.

    Returns:
        The escaped string.
    """

    # TODO: we may need to re-vist this logic after new Var API is implemented.
    def escape_outside_segments(segment: str):
        """Escape backticks in segments outside of `${}`.

        Args:
            segment: The part of the string to escape.

        Returns:
            The escaped or unescaped segment.
        """
        if segment.startswith("${") and segment.endswith("}"):
            # Return the `${}` segment unchanged
            return segment
        # Escape backticks in the segment
        return segment.replace(r"\`", "`").replace("`", r"\`")

    # Split the string into parts, keeping the `${}` segments
    parts = re.split(r"(\$\{.*?\})", string)
    escaped_parts = [escape_outside_segments(part) for part in parts]
    return "".join(escaped_parts)
Escape the string for use as a JS string literal.

    Args:
        string: The string to escape.

    Returns:
        The escaped string.
    
reflex/utils/format.py