gsubbing a string with a pattern containing a newline character in Lua

Viewed 2607

Does string.gsub recognize the newline character in a string literal? I have a scenario in which I am trying to gsub a portion of a string indicated by a given operator from the start of the operator to the newline like so:

local function removeComments(str, operator)
    local new_Sc = (str):gsub(operator..".*\n", "");
    return new_Sc;
end
local source = [[
int hi = 123; //a basic comment
char ok = "abc"; //another comment
]];
source = removeComments(source, "//");
print(source);

however in the output I see that it removed the rest of the string literal after the first comment:

int hi = 123; 

I tried using the literal newline character by using string.char(10) like so (str):gsub(operator..".*"..string.char(10), ""); however I still got the same output; it removes the comment and the rest of the string instead of the start of the comment to the newline.

So is there anyway to gsub a string literal for a pattern containing a newline character?

Thanks

1 Answers
Related