Lua - 获取indexOf字符串

时间:2013-11-26 16:43:43

标签: string lua find indexof

我在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..

2 个答案:

答案 0 :(得分:12)

正如已经提到的,-是一种模式元字符,specifically

  
      
  • 单个字符类后跟' - ',它还匹配类中0个或更多个字符的重复。与'*'不同,这些重复项总是匹配最短的序列;
  •   

您可能对string.findplain选项感兴趣。这将避免将来逃避任何其他事情的需要。

string.find('no-cache', 'no-cache', 1, true)

答案 1 :(得分:5)

- 是lua中的模式元字符。你需要逃脱它。 string.find('no-cache', 'no%-cache')

相关问题