我在Lua中遇到问题,检查字符串值是否未在另一个字符串中显示.
这就是我在Javascript中可能会这样做的方式:
'my string'.indexOf('no-cache') === -1 // true
但在Lua我试图使用字符串模块,这给了我意想不到的响应:
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;
您可能对string.find
的普通选项感兴趣.这将避免将来转义任何其他内容.
string.find('no-cache','no-cache',1,true)