Check a string stored in a variable.

Hi!
How can I check how many occurrences of a word I have in a string stored in a variable?
I have a plugin that can store a free input txt in a variable.
I'm using this script (in Conditional Branches) to check what i wrote ($gameVariables.value(N).toLowerCase().includes("word")).
But how can I check how many time "word" recurs in the string?
Any ideas?
Thanks^^
How can I check how many occurrences of a word I have in a string stored in a variable?
Checks for the word apple in variable #1 and strores the number of how many matches in variable #2
Code:
$gameVariables.setValue(2, ($gameVariables.value(1).match(/apple/g) || []).length)

eg.
if Variable #1 was "apple apple orange apple grape banana lemon" the above would return 3
Checks for the word apple in variable #1 and strores the number of how many matches in variable #2
Code:
$gameVariables.setValue(2, ($gameVariables.value(1).match(/apple/g) || []).length)

eg.
if Variable #1 was "apple apple orange apple grape banana lemon" the above would return 3
Thank you, sir!
Thank you, sir!
No prob 🙂

However there is a flaw in that method...
what if you wanted to use a variable instead of the hardcoded word 'apple'...

...well never fear got that covered too 😉
Created a very small plugin for this 🙂

To use just add via the plugin manager:
Spoiler
1585844784227.png
Event:
Spoiler
1585844352102.png
1585844731170.png
CT, coming through with the js help, as usual. You're awesome!

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/check-a-string-stored-in-a-variable.119731/