0

I have my_string of my_text_field (but its hidden), for example, FRANCE USA_ILANDS GERMANY (space between these each country / words.....actually, all these country names are NAMES of text fields/drop-downs/check boxes on my_form) like that i am concatenating around 200 countrries, well.

Now, i want to search for a match in my_string, say for example, am looking to search for a match of USA, so i put the below JS,

var myCountryName = /USA/; 

var returnValue = my_text_field.search(myCountryName);

if(returnValue != -1){; // This Country/field/object is found in the my_text_field, hence greyed out with readOnly
this.ui.oneOfChild.border.fill.color.value = "192,192,192";
this.access = "readOnly";
};

I am expecting the return value should be false.

But, its coming as true!

https://stackoverflow.com/questions/447250/matching-exact-string-with-j avascript

Am following the first suggested option with place holders like, var r = /^a$/ its working fone for me

Pls. let me know is this is safe? ok? recommendabale? or Any other help to find out exact match word?

Thank you

Community
  • 1
  • 1
user1754195
  • 41
  • 1
  • 1
  • 6

2 Answers2

2

Use \b to check for word boundaries on each side.

/\bUSA\b/
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Thank you. Actually, i put it as in my original question as /^USA$/ and already integration team put in the list for sending it to production.....so, do you think my syntax is not safe? if not safe, now i need to coordinate with integration folks to ask to stop sending my change to production and i need to implement your suggestion (\b \b)....pls. let me know, mine is not safe? Thank you – user1754195 Oct 17 '12 at 20:26
0

If you want to know if USA exists then why not use:

my_text_field = ' '+my_text_field+' ';
my_text_field.indexOf(' '+myCountryName+' ');

Unless I'm missing something.

Bryan Allo
  • 696
  • 3
  • 9
  • And if it is the first item or last? – epascarello Oct 17 '12 at 20:21
  • Good point. I edited the code to pad the string. I guess my bigger point is that indexOf() is probably a more efficient way of getting the same simple result. – Bryan Allo Oct 17 '12 at 20:29
  • Your welcome. Sometimes it's the simple things that get you. :-) – Bryan Allo Oct 17 '12 at 20:32
  • Today i posted a question.....but no body is answering...is it bcz of my reputation went minus (-2)? so, once user goes minus reputation, then nobody will asnwer? My question is....................http://stackoverflow.com/questions/12955956/how-to-check-the-object-existence-validity – user1754195 Oct 18 '12 at 14:50