0

As explained in How do you search multiple strings with the .search() Method?, you can use multiple arguments inside .search().

But how can I know which of the arguments given inside .search() returns a positive value?

E.g:

var search = string.search("hello|world|not");

if (search.positiveArgument == 1) {
    //"hello" was found
} else if (search.positiveArgument == 2) {
    //"world" was found
etc...

I am aware that .positiveArgument isn't valid, it is purely for example purposes.

Community
  • 1
  • 1
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87

1 Answers1

1

Use .match() instead with a regular expression. The matched part will be returned.

For ex: "yo world".match(/hello|world|not/); returns ["world"]

var search = str.match(/hello|world|not/);

if (search && search.length === 1) {
    if(search[0] == "hello") {
        // hello was found
    }
    else if(search[0] == "world") {
        // world was found
    }
    else {
        // not was found
    }
}
Aleksander Azizi
  • 9,829
  • 9
  • 59
  • 87
techfoobar
  • 65,616
  • 14
  • 114
  • 135