简体   繁体   中英

gsub in Lua. Unable to replace pattern

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

I write 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?

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

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

If you want to grab the whitespace around $br$ as well:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM