'Search for a whole word in a string

I am looking for a function written in JavaScript (not in jQuery) which will return true if the given word exactly matches (should not be case sensitive).

Like...

var searchOnstring = " Hi, how are doing?"

   if( searchText == 'ho'){
     // Output: false
   }

   if( searchText == 'How'){
     // Output: true
   }


Solution 1:[1]

Here is a function that returns true with searchText is contained within searchOnString, ignoring case:

function isMatch(searchOnString, searchText) {
  searchText = searchText.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  return searchOnString.match(new RegExp("\\b"+searchText+"\\b", "i")) != null;
}

Update, as mentioned you should escape the input, I'm using the escape function from https://stackoverflow.com/a/3561711/241294.

Solution 2:[2]

Something like this will work:

if(/\show\s/i.test(searchOnstring)){
    alert("Found how");
}

More on the test() method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2 faino