I'm having trouble sanitizing bbcode text for a RichTextLabel node. I looked at this Reddit post, but it doesn't seem to work. I've tried pasting the character or using a \u200b in my escape function, and I think that the post was for Godot 2.0 which I have never used. This is my code:

#In singleton called Util
func bbcode_escape(text:String):
	var out = text
	out.replace("[", "​[\u200b")
	out.replace("]", "\u200b]​")
	return out

# In my test:
var print_cache = []
func _ready():
    println(Util.bbcode_escape("[color=red]RED[/color]"))

func println(text:String, color:Color=Color.white):
	var formatText = printc(text, color, false)
	$out.bbcode_text += formatText

func printc(text:String, color:Color=Color.white, doCache:bool=true):
	if !text:
		return ""
	var formatText = "\n[color=#"
	formatText += color.to_html(false)
	formatText += "]"
	formatText += text
	formatText += "[/color]"
	if doCache:
		print_cache.append(formatText)
	return formatText

I've tried bbcode_text += and .append_bbcode(). I'm not sure what's wrong.

The issue is on lines 4 and 5. String.replace() returns a String – it does not work in place, so you need to assign its result to a variable (such as out itself).

3 months later

This also caused me to stumble upon an amazing character, great for tripping up inputs and what-not. For example: :​D should normally be :D but I added ZWSP in-between. I can also put it in where an input expects to contain text to mess things up for no reason.

Here's a useful application.

func _ready() -> void:
	print_debug('The length of "x​y" is ', len("x​y"))

outputs:

The length of "x​y" is 3

Yeah, that's how you mess everything all the way up! :​D

a year later