我在 Lua 中遇到了一个问题,以检查字符串值是否未出现在另一个字符串中。
这就是我可能会在 Javascript 中这样做的方式:
'my string'.indexOf('no-cache') === -1 // true
但在 Lua 中,我尝试使用
string
给我意外响应的模块:string.find('my string', 'no-cache') -- nil, that's fine but..
string.find('no-cache', 'no-cache') -- nil.. that's weird
string.find('no-cache', 'no') -- 1, 2 here it's right.. strange..
最佳答案
如前所述,-
是一个模式元字符,specifically :
- a single character class followed by '-', which also matches 0 or more repetitions of characters in the class. Unlike '*', these repetition items will always match the shortest possible sequence;
您可能对
plain
感兴趣 string.find
的选项.这将避免将来逃避其他任何事情的需要。string.find('no-cache', 'no-cache', 1, true)
关于string - Lua - 获取 indexOf 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20222916/