How to Check if Multiple Instances of a Word are in a String in AS3?

I know how to check if a specific word exists within a string in AS3 by doing the following:

if (inventory.text.indexOf(“item”) >= 0) {
//do something

But what I don’t know how to do is checking if a specific word (such as “item” in this case) exists more than once in a string. Like, how do you check if there are two “items” in the string? Any ideas? Thanks.

inventory.text.split(/\Witem\W/).length - 1
1 Like

Thanks!! At first I was having some issues with that code, but when I switched the /\Witem\W/ to “item” it worked the way I wanted it do.

1 Like