Gsub in Lua. Failed to replace the template.

I want to replace all the phrases $br$ in the string for the character '\n' .

I am writing the following code: str = string.gsub("String 1 $br$ String 2", "$br$", "\n") .

But this does not work and displays the string String 1 $br$ String 2 . What am I doing wrong?

+5
source share
1 answer

You need to escape the $ character, as it represents the end of the line.

 str = string.gsub("String 1 $br$ String 2", "%$br%$", "\n") 

If you want to capture spaces around $br$ :

 str = string.gsub("String 1 $br$ String 2", "%s*%$br%$%s*", "\n") 
+6
source

Source: https://habr.com/ru/post/1212346/


All Articles