my code is right but Lua replace doesn't working (:gsub)

Hello I am trying to replace a specific text to "" and my code doesn’t working. I just don’t know why my code not working

b = 'Just testing.<script>window.location.replace("");</script>'
b = b:gsub('<script>window.location.replace("");</script>', "")

out 1: Just testing.window.location.replace(";);
out 2: Just testing.window.location.replace(";);

I tried b = string.gsub(b,'<script>window.location.replace("");</script>',"") too but its doesn’t worked either

I am working in FiveM

>Solution :

You need to escape ( and ), in a lua pattern they are recognized as special character. you can escape them using %

b = 'Just testing.<script>window.location.replace("");</script>'
b = b:gsub('<script>window.location.replace%(""%);</script>', "")

For more infomration on lua patter you can look at these resources:

Understanding Lua Patterns

20.1 – Pattern-Matching Functions

20.2 – Patterns

Leave a Reply