How does the "Match Whole Word" feature in Find All\Replace All widget work?

How does the “Match Whole Word” feature in Find All\Replace All widget work?
изображение


I put this in the #help-and-feedback:scripting-support because I need to implement the same feature for my plugin.

Found a solution.

SearchText = MatchWholeWord and " "..SearchText:gsub("^%s", ""):gsub("%s$", "").." " or SearchText

Normally in a regex engine, you have \b which means “word boundary”, which is any point between a word character (\w) and a non-word character.
In JavaScript, the regex \btest\b would match the word “test”, but not “tested” or “contest”.

There is no direct counterpart to this in Lua 5.1 other than the undocumented “frontier pattern”, which I’m not 100% sure is included in Luau.
So yes, your best bet is to space-pad your search text (you don’t have to remove the trailing/leading spaces first btw), then search for %Wtest%W (the word test, surrounded by any non-word characters) to find the word test.

1 Like