Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. -- USAGE:
  2. -- string.replace("mystring", "my", "our")
  3. -- or
  4. -- local teststr = "weird[]str%ing"
  5. -- teststr2 = teststr:replace("weird[]", "cool(%1)")
  6.  
  7. -- Warning: add your own \0 char handling if you need it!
  8.  
  9. do
  10. local function regexEscape(str)
  11. return str:gsub("[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%1")
  12. end
  13. -- you can use return and set your own name if you do require() or dofile()
  14.  
  15. -- like this: str_replace = require("string-replace")
  16. -- return function (str, this, that) -- modify the line below for the above to work
  17. string.replace = function (str, this, that)
  18. return str:gsub(regexEscape(this), that:gsub("%%", "%%%%")) -- only % needs to be escaped for 'that'
  19. end
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement