0

I want to replace a word into my body content from other string .
To implement this i am using ngx.re.sgub but i am getting a weird issue. ngx.re.gsub is not handling magic characters.

  • Example :

    content1 = "HiTestHello Test how are you Testall "
    _ssi = "Test"
    body = "$100.00"
    content2 = ngx.re.gsub(content1, _ssi, body)
    ngx.print(content2)
    

output is

Hi.00lHelo .00 how are you .00all he.00llo .00 how are you .00all

while output should like :

Hi$100.00Hello .00 how are you .00all.

Please let me know how can i achieve this .

Prashant Gaur
  • 9,540
  • 10
  • 49
  • 71

2 Answers2

1

In ngx regex, $1, $2, etc. are variable to be captured. Try escape the $ character:

body = "$$100.00"
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • value of body is dynamic which is coming from my cms and i can not make changes there . Please help me to find out other way to fix this . – Prashant Gaur Aug 21 '14 at 07:35
  • @PrashantGaur At least try to see if it's this issue because I don't have an environment to test. – Yu Hao Aug 21 '14 at 07:40
  • As per your suggestion if i am putting body = $$100.00 it is working fine . i think i can parse body data and look if any $ or other character exists then i will convert $ into $$ . – Prashant Gaur Aug 21 '14 at 07:43
  • 1
    You can wait to see if others can provide a native solution to that, but your method sounds fine to me. – Yu Hao Aug 21 '14 at 07:51
0

Wrap the body with a function also avoids it:

content1 = "HiTestHello Test how are you Testall "
_ssi = "Test"
body = "$100.00"
content2 = ngx.re.gsub(content1, _ssi, function()
    return body
end, "o")
ngx.print(content2)
罗泽轩
  • 1,603
  • 2
  • 14
  • 19