0

Using javascript match to check if a $.get request contains a string, how do I get the actual string matched? Ideally the match should only return true if the exact phrase is found. My code:

var word = "My Phrase";
/// data = "My Phrases, string2, another phrase, etc"
var mymatch = data.match(word);

I dont want mymatch to match data unless it is an exact match, in this case mymatch should not match any of the data provided.

I was hoping to do an exact match on the data OR get the string matched and check if they were the same. i.e.

if (mymatch == whatwasmatched) { then an exact match was found }

Am I just missing some positioning element in my match or can I find what was matched by match()?

Laurence Tuck
  • 410
  • 2
  • 8
  • 30
  • What's the problem with `if(mymatch == whatwasmatched)` ? If you want to provide if it's exactly the same, so the same object or whatsoever use `if(mymatch === whatwasmatched)`. – bastianwegge Apr 26 '11 at 13:50
  • This question might help http://stackoverflow.com/questions/447250/matching-exact-string-with-javascript – realshadow Apr 26 '11 at 13:52
  • @wegginho: I dont know what was matched - it could be a number of variations, match may return numerous matches too – Laurence Tuck Apr 26 '11 at 13:55
  • fortunately my "data" is a dropdown option list with values as well as strings - Ive worked around my issue using the values as these are unique... match(/^word$/) does not work as it should?? – Laurence Tuck Apr 26 '11 at 14:39

0 Answers0