Jump to content

string.fin/gsub


#Paper

Recommended Posts

Posted

i have this code:

if string.find(g_MapInfo.name, "[DM]", 1, false) then 
   g_MapInfo.name = string.gsub(g_MapInfo.name, '[DM]', '#ff0000[DM]#236B8E') 
end 

instead to be: [DM] lol

it becomes: [[DM][DM]] lol

How to fix?

Posted

try

if string.find(g_MapInfo.name, "[DM]", 1, true) then -- disable pattern matching on [ ] 
   g_MapInfo.name = string.gsub(g_MapInfo.name, '%[DM%]', '#ff0000[DM]#236B8E') -- escape [ ] with %[ %] 
end 

Posted
try
if string.find(g_MapInfo.name, "[DM]", 1, true) then -- disable pattern matching on [ ] 
   g_MapInfo.name = string.gsub(g_MapInfo.name, '%[DM%]', '#ff0000[DM]#236B8E') -- escape [ ] with %[ %] 
end 

omfg it works D:

Btw what changes if i use the %?? D:

Posted

[ and ] are special characters used to denote a character set in a pattern. "[DM]" is a pattern and it means "D or M".

that's why special characters must be escaped with %.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...