Lua - get string indexOf

I am stuck with a problem in Lua to check if a string value is indicated on another string.

As I will probably do this in Javascript:

'my string'.indexOf('no-cache') === -1 // true 

but in Lua, I'm trying to use the string module, which gives me an unexpected answer:

 string.find('my string', 'no-cache') -- nil, that fine but.. string.find('no-cache', 'no-cache') -- nil.. that weird string.find('no-cache', 'no') -- 1, 2 here it right.. strange.. 
+7
string find lua indexof
source share
2 answers

As already mentioned, is the template metacharacter:

  • one character class followed by '-', which also corresponds to 0 or more repetitions of characters in the class. Unlike "*", these repetition elements will always correspond to the shortest possible sequence;

You might be interested in the plain option for string.find . This avoids the need to avoid anything else in the future.

 string.find('no-cache', 'no-cache', 1, true) 
+12
source share

- is a template metacharacter in lua. You need to avoid this. string.find('no-cache', 'no%-cache')

+5
source share

All Articles