6

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
   }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anand Jha
  • 10,404
  • 6
  • 25
  • 28

4 Answers4

21

You could use regular expressions:

\bhow\b

Example:

/\bhow\b/i.test(searchOnstring);

If you want to have a variable word (e.g. from a user input), you have to pay attention to not include special RegExp characters.

You have to escape them, for example with the function provided in the MDN (scroll down a bit):

function escapeRegExp(string){
  return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

var regex = '\\b';
regex += escapeRegExp(yourDynamicString);
regex += '\\b';

new RegExp(regex, "i").test(searchOnstring);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ComFreek
  • 29,044
  • 18
  • 104
  • 156
  • it shows error like: Uncaught TypeError: Object function RegExp() { [native code] } has no method 'escape' – Anand Jha Sep 11 '13 at 12:25
  • @anand4tech You have to include that function before calling my code. /Thanks Doorknob. – ComFreek Sep 11 '13 at 12:28
  • This escape function is directly taken from https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions – Denys Séguret Sep 11 '13 at 12:28
  • @anand4tech See my edit, I used the escape function from MDN, now. /dystroy: Thanks. – ComFreek Sep 11 '13 at 12:31
  • @ComFreek, The value of ---new RegExp(regex, "i").test(searchOnstring);--- is coming is always false – Anand Jha Sep 11 '13 at 12:40
  • function escapeRegExp(string){ return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); } function myFunction() { var yourDynamicString='how Are you'; var searchOnstring='how'; var regex = '\b'; regex += escapeRegExp(yourDynamicString); regex += '\b'; alert(regex) var y= new RegExp(regex, 'i').test(searchOnstring); alert(y) } – Anand Jha Sep 11 '13 at 12:41
  • @anand4tech 1. You confused *yourDynamicString* with *searchOnstring*. 2. One must escape '\' even in single quotes (my fault, sorry). 3. Please use semicolons. Working example: http://jsfiddle.net/Ydr3R/ – ComFreek Sep 11 '13 at 12:51
  • Is the example valid JavaScript code (not a rhetorical question)? – Peter Mortensen Feb 08 '22 at 13:49
  • @PeterMortensen Which example do you mean exactly? Can you give me a hint on what should be invalid syntax? (I wrote this answer 9 years ago :)) – ComFreek Feb 11 '22 at 13:05
3

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.

Community
  • 1
  • 1
poida
  • 3,403
  • 26
  • 26
  • This is a link-only answer, which is discouraged. What happens if the link breaks? Your answer becomes useless. Also, we don't want people having to click *yet another* link after they arrive at SO. -1 – tckmn Sep 11 '13 at 12:14
  • @poida You should escape the `searchText`. – ComFreek Sep 11 '13 at 12:23
  • Now it's just wrong. And why are you checking for `null` anyway? – tckmn Sep 11 '13 at 12:23
  • Did you try it? (better now because of escape. -1 removed) – tckmn Sep 11 '13 at 12:51
  • @poida Removed my downvote, too, but one should really move the escape replacing into a function (I know, this is rather stylistic). – ComFreek Sep 11 '13 at 13:10
-2

Something like this will work:

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

More on the test() method

faino
  • 3,194
  • 15
  • 17
-5

Try this:

var s = 'string to check', ss= 'to';
if(s.indexOf(ss) != -1){
  //output : true
}