SHARE
TWEET

Untitled

a guest Apr 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top