5
To find only a fixed value in a string I can use the Javascript function indexOf(), example:
var string = "Oberyn se vinga";
string.indexOf('vinga') >= 0 //retorna true ou false
But how would I check multiple values against a string? Example:
var string = "Oberyn se vinga de Sor Clegane";
string.indexOf(['vinga','de','Clegane'])
Apparently the function indexOf() does not accept arrays in the search.
The only way would be using regex? Or is there a specific function for these cases?
You want to receive
trueif at least one of the values was found? Or something more complex (as offset of each value)?– bfavaretto
@bfavaretto had not assailed me for these two possibilities, but I think the offset of each value would be more interesting, since with it already gives to check if returned something or not.
– Kazzkiq
It is worth noting
indexOf()returns the substring index in the original string. Does not returntrueorfalse. In fact, in your example the comparison will returnfalsebecausestring.indexOf('vinga')is 10. The ideal comparison would bestring.indexOf('vinga') >= 0, forindexOf()will return -1 if it does not find the index. Besides... hey, look at the spoiler, man :P– brandizzi
@brandizzi well remembered, corrected there. On the spoiler, it may or may not be, depends on the point of view, haha
– Kazzkiq