Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  151] [ 5]  / answers: 1 / hits: 21425  / 13 Years ago, mon, march 21, 2011, 12:00:00

How to exactly match a given string in a sentence.



For example if the sentence is
var sentence = A Google wave is basically a document which captures a communication



and the given string is
var inputString= Google Wave. I need to check the exact presence of Google Wave in the above sentence & return true or false.



I tried



if(sentence.match(inputString)==null){
alert(No such word combination found.);
return false;
}


This works even if someone enters Google W. I need a way to find the exact match. Please help


More From » javascript

 Answers
55

OP wants to return false when do search with Google W.



I think you should use word boundary for regular expression.



http://www.regular-expressions.info/wordboundaries.html



Sample:



inputString = \b + inputString.replace( , \b \b) + \b;
if(sentence.toLowerCase().match(inputString.toLowerCase())==null){
alert(No such word combination found.);
}

[#93161] Saturday, March 19, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
robertjaysona

Total Points: 276
Total Questions: 110
Total Answers: 116

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
;