Searching Lua with string.gsub?

With the Lua function, string.findthere is an optional fourth argument that you can pass to enable a simple search. From the Lua wiki :

The pattern argument also allows more complex searches. See PatternsTutorial for more information. We can disable the template with the optional fourth argument. smooth takes a boolean value and should prevail by index. For instance.

= string.find("Hello Lua user", "%su")         -- find a space character followed by "u"

10      11

= string.find("Hello Lua user", "%su", 1, true) -- turn on plain searches, now not found

nil

Basically, I was wondering how I can perform the same simple search using the Lua function string.gsub.

+4
source share
3 answers

, - , . , , , .

:

  • %, (, % %%, [ %[
+2

:

function string.replace(text, old, new)
  local b,e = text:find(old,1,true)
  if b==nil then
     return text
  else
     return text:sub(1,b-1) .. new .. text:sub(e+1)
  end
end

newtext = text:replace(old,new).

, old text.

+2

, ( ) .

function escape_magic(s)
  return (s:gsub('[%^%$%(%)%%%.%[%]%*%+%-%?]','%%%1'))
end
0

All Articles