11

I am stuck with a problem in Lua to check whether the string value is not presented in another string.

That's how I likely will do it in Javascript:

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

but in Lua I'm trying to use string module which gives me unexpected response:

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..
Yu Hao
123k50 gold badges252 silver badges305 bronze badges
asked Nov 26, 2013 at 16:43

2 Answers 2

16

As has already been mentioned, - is a pattern metacharacter, 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;

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

string.find('no-cache', 'no-cache', 1, true)
answered Nov 26, 2013 at 16:57
Sign up to request clarification or add additional context in comments.

Comments

9

- is a pattern metacharacter in lua. You need to escape it. string.find('no-cache', 'no%-cache')

answered Nov 26, 2013 at 16:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.