Help merging two scripts

Hi,
I'm using these two scripts in conditional branches (with a free input text plugin that store the string inserted by the player in a variable):

1) let text = $gameVariables.value(x).toLowerCase(); text.indexOf("word 1") < text.indexOf("word 2")

2) (function(){ words = ["word"]; for(i=0;i<words.length;i++){if(!RegExp("\\b"+words+"\\b").test($gameVariables.value(x).toLowerCase())){break}};if(words.length===i)return!0})()

1) allows me to check if a given "word 1" occurs in the string before a given "word2"

2) allows me to check if in the string occurs a very specific word: i.e. "head" BUT NOT "headache".

Now...I need to merge these two: I need to check if a very specific "word1" occurs before a given "word2".
Someone more skilled than me in javascript can help? 😢
Hi there!
Let me see if I understand you correctly before I try to help.

You want to check in a conditional branch the value of a variable that is a string.
And if that string is a word, like:
'head'

But, if this variable value is 'headache', do you want to check first if it is 'head'?
Did I get it?

Maybe you can try to explain what you are trying to do in your game than your javascript code?
Like, is this a password thing?
(Also, try to put your javascript code inside the code tag, it helps for reading ^^
Hi there!
Let me see if I understand you correctly before I try to help.

You want to check in a conditional branch the value of a variable that is a string.
And if that string is a word, like:
'head'

But, if this variable value is 'headache', do you want to check first if it is 'head'?
Did I get it?

Maybe you can try to explain what you are trying to do in your game than your javascript code?
Like, is this a password thing?
(Also, try to put your javascript code inside the code tag, it helps for reading ^^

The variable can contain a whole phrase.
I.e. Var(x)="Today my head hurts, I have an headache."

Now, with script 1) I can check if "hurts" come first than "have". Or "Today" comes first than "my" etc etc.

Script 2) can check if the phrase contain "head" (but it won't return true for "headache").

The problem is that if I use the 1) script to check, for example, if "head" comes before another given word, it actually check even for compunds words like "headache" and I don't want this happen.
I want it checks ONLY for "head" like script 2) does.
It must check if only "head" exist in the phrase (ignoring "headache") and comes before "word2".
From here the need to "hybridate" the 2 scripts...
I hope this is more clear now...
Try this, replacing x and y with the variable numbers containing the words you want to check and 'Some phrase' with whatever phrase you want to check the words for (or another $gameVariables.value(z), with z being the phrase you want to check):

Code:
const word1=$gameVariables.value(x);const word2=$gameVariables.value(y);new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');

As an example:
Code:
// returns true because "head" occurs before "splitting", ignoring case sensitivity
const word1='HeAd';const word2='SpLiTTiNg';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe'); 

// returns false because "splitting" does not occur before "head", ignoring case sensitivity
const word1='SpLiTTiNg';const word2='HeAd';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');
Moderator note: Moved to Learning JavaScript
This board generally has the best answers for questions like this.
Try this, replacing x and y with the variable numbers containing the words you want to check and 'Some phrase' with whatever phrase you want to check the words for (or another $gameVariables.value(z), with z being the phrase you want to check):

Code:
const word1=$gameVariables.value(x);const word2=$gameVariables.value(y);new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');

As an example:
Code:
// returns true because "head" occurs before "splitting", ignoring case sensitivity
const word1='HeAd';const word2='SpLiTTiNg';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');

// returns false because "splitting" does not occur before "head", ignoring case sensitivity
const word1='SpLiTTiNg';const word2='HeAd';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('ToDaY my HeaD HURTS, I haVe A sPlITTinG HeaDAcHe');
YAY! This is what I need, just a thing; it seems not work when I try to check two differents words as "word1".
Like this:

Code:
const word1=("x" || "y");const word2=("z");new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('Some phrase');

There is a way to do that?
For the rest it works perfectly
Thank you^^
Short answer: Try assigning something like const word1='x|y' instead, using the | character to separate each word you want to check, but leave it all as a single String, e.g. 'x|y|z|a|b'.

Since we're in the Learning JS board now, a longer answer:

In programming, the || (or) and && (and) operators are supposed to be used with Boolean (true/false) statements only. In a lot of languages, ("x" || "y") would actually throw errors instead of executing.

JavaScript has a feature called "type coercion", however, where if you use unexpected values for an operation, the language attempts to coerce (convert) them into the appropriate values. So the way JS handles || statements is to say, "Use the first value that's true or close enough to true". The question, then, is "Is the String 'x' true or false?" As a general rule, anything that isn't an empty String (''), undefined, null, or the number 0 is true as far as JS is concerned.

So JS will look at that statement and say, "OK, of the values 'x' and 'y', use the first one that's true. Well, 'x' is close enough to true, so we'll use that," and word1 will always equal "x".

There is a very slight change we can make, however, for your purposes:
Code:
// returns true because either a or b or c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('c d a b e');

// returns false because neither a nor b nor c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('d e a b c ');

In a regular expression (the RegExp object we're making), you can use the | character to act as an "or" in a manner more akin to how you might expect ("x" || "y") to work. So we're checking if a or b or c occur before the word d.

If you want to use $gameVariables.value for all your possible words, there are a couple ways to do so:
Code:
// concatenate all of them in using template literals
const word1=`${$gameVariables.value(x)}|${$gameVariables.value(y)}|${$gameVariables.value(z)}`;

// concatenate all of them using a plus sign
const word1=$gameVariables.value(x) + '|' + $gameVariables.value(y) + '|' + $gameVariables.value(z);

// Use the Array join method to do it
const word1=[$gameVariables.value(x), $gameVariables.value(y), $gameVariables.value(z)].join('|');

Since that can get a bit lengthy, if you run out of room, you could also assign $gameVariables.value to a short variable as an alias:
Code:
// with template literals
const _=$gameVariables.value;const word1=`${_(x)}|${_(y)}|${_(z)}`;

// with plus signs
const _=$gameVariables.value;const word1=_(x) + '|' + _(y) + '|' + _(z);

// With Array join
const _=$gameVariables.value;const word1=[_(x), _(y), _(z)].join('|');
Short answer: Try assigning something like const word1='x|y' instead, using the | character to separate each word you want to check, but leave it all as a single String, e.g. 'x|y|z|a|b'.

Since we're in the Learning JS board now, a longer answer:

In programming, the || (or) and && (and) operators are supposed to be used with Boolean (true/false) statements only. In a lot of languages, ("x" || "y") would actually throw errors instead of executing.

JavaScript has a feature called "type coercion", however, where if you use unexpected values for an operation, the language attempts to coerce (convert) them into the appropriate values. So the way JS handles || statements is to say, "Use the first value that's true or close enough to true". The question, then, is "Is the String 'x' true or false?" As a general rule, anything that isn't an empty String (''), undefined, null, or the number 0 is true as far as JS is concerned.

So JS will look at that statement and say, "OK, of the values 'x' and 'y', use the first one that's true. Well, 'x' is close enough to true, so we'll use that," and word1 will always equal "x".

There is a very slight change we can make, however, for your purposes:
Code:
// returns true because either a or b or c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('c d a b e');

// returns false because neither a nor b nor c occur before d
const word1='a|b|c';const word2='d';new RegExp(`(\\b${word1}\\b).*(\\b${word2}\\b)`, 'i').test('d e a b c ');

In a regular expression (the RegExp object we're making), you can use the | character to act as an "or" in a manner more akin to how you might expect ("x" || "y") to work. So we're checking if a or b or c occur before the word d.

If you want to use $gameVariables.value for all your possible words, there are a couple ways to do so:
Code:
// concatenate all of them in using template literals
const word1=`${$gameVariables.value(x)}|${$gameVariables.value(y)}|${$gameVariables.value(z)}`;

// concatenate all of them using a plus sign
const word1=$gameVariables.value(x) + '|' + $gameVariables.value(y) + '|' + $gameVariables.value(z);

// Use the Array join method to do it
const word1=[$gameVariables.value(x), $gameVariables.value(y), $gameVariables.value(z)].join('|');

Since that can get a bit lengthy, if you run out of room, you could also assign $gameVariables.value to a short variable as an alias:
Code:
// with template literals
const _=$gameVariables.value;const word1=`${_(x)}|${_(y)}|${_(z)}`;

// with plus signs
const _=$gameVariables.value;const word1=_(x) + '|' + _(y) + '|' + _(z);

// With Array join
const _=$gameVariables.value;const word1=[_(x), _(y), _(z)].join('|');
Veeery helpful, thank you a lot!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/help-merging-two-scripts.123899/