Reputation: 23
I have an input box and that's working great, but my issue is with the script that checks to see if certain words were included in it. Right now I have:
$gameVariables.value(1).toLowerCase().includes("steal") || $gameVariables.value(1).toLowerCase().includes("thief") || $gameVariables.value(1).toLowerCase().includes("pickpocket")
This is working fine except I would prefer it to work for whole words only. How would I go about doing that?
I was also wondering if there's a way to shorten this script so I don't have to copy and paste to same line for every word. I've tried the following code (which doesn't work. It only works for the first word but not the ones that come after.):
$gameVariables.value(1).toLowerCase().includes("steal","thief","pickpocket")
Upvotes: 0
Views: 114
Reputation: 1584
If you only want the words which exactly matches either theif
or steal
or pickpocket
you can use the following code
const value = $gameVariables.value(1);
const regex = /^(thief|steal|pickpocket)$/i;
return regex.test(value);
So to explain the regex above,
^
will make sure that the regex matches from the start(
and )
creates a group for which regex should match|
is like or operator in coding$
is used when you want the end to exactly match with the end of the regexi
basically is a flag to tell that you ignore the case while doing the check. So you won't need to use the .toLowerCase()
either hereSo the code above would basically check if the word is exactly matching either theif
or steal
or pickpocket
and would return false
if the word is theifs
or theif steal
But if you don't want the exact match and only want to know if the input value contains these words then the simple answer would be to not use ^
and $
const value = $gameVariables.value(1);
const regex = /(thief|steal|pickpocket)/i;
return regex.test(value);
And the result would be that it would be true for exact matches like thief
, steal
and pickpocket
and it would also be true when either of the word is present in the whole statement like this statement contains thief
. The second method would fail for words like the
or stl
. But the problem is that it would also match with steals
or would return true for statements like this statement contains steals
. To fix it you can use the following code
const value = $gameVariables.value(1);
const regex = /(thief|steal|pickpocket)\b/i;
return regex.test(value);
\b
allows you to perform a “whole words only” searchthus, it would return false if the statement contains this statement contains steals
but would return true when the statement contains this statement contains steal
or word which exactly matches steal
Upvotes: 2
Reputation: 2250
You can define an array ([]
) of words to look for and then do this:
// Your list of words to look for
const words = ["steal", "thief", "pickpocket", "quick"];
// Your string
const str = "The quick brown fox jumps over the lazy dog";
const result = words.some(w => str.includes(w));
console.log(result);
Array.prototype.some
will return true
if any of the words are found in your string.
Upvotes: 0