传送门https://github.com/ccppluagopy/SensitiveWord-Lua
转这个位置已经写的很棒了,然后稍微修改一下,改用小写来检测,原文替换保留大写的原文
function MsgParser:getString(s)
if type(s) ~= 'string' then return end
local i = 1
local len = strLen(s)
local word, idx, tmps
local returnStr = s;
s = string.lower(s);
while true do
word = strSub(s, i)
idx = _detect(_tree, word, i)
if idx then
tmps = strSub(s, 1, i-1)
for j=1, idx-i+1 do
tmps = tmps .. _maskWord
end
s = tmps .. strSub(s, idx+1)
tmps1 = strSub(returnStr, 1, i-1)
for j=1, idx-i+1 do
tmps1 = tmps1 .. _maskWord
end
returnStr = tmps1 .. strSub(returnStr, idx+1)
i = idx+1
else
i = i + 1
end
if i > len then
break
end
end
return returnStr
end
这样我们使用就已经ok了