1

following code works fine

var srch_str = "snow"
var str = "snow Showers Late"
alert((new RegExp(srch_str)).test(str))   //true

but this one not , i could not get why ?

var weather_status = ['rain', 'cloudy', "snow", 'wind', 'thunderstorms' ]
    function getStatus(str){

        for(srch_str in weather_status){


            var bool = (new RegExp(srch_str)).test(str)
            if(bool){
                str = srch_str
                Ti.API.info("if ......")
                break;
            }
            else{
                Ti.API.info(" else ----------")
            }

        }//for

        return str
    }

output : else ------------ (5 times)

when calling getStatus("snow Showers Late")

navyad
  • 3,752
  • 7
  • 47
  • 88

1 Answers1

4

When you loop with for(srch_str in weather_status) the srch_str is the index of the array, not the element at that index. Change the regex line to:

var bool = (new RegExp(weather_status[srch_str])).test(str);

Demo: http://jsfiddle.net/q688j/

(Debugging tip: console.log( srch_str ) would have pointed out the problem right away.)

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • Works for me. (You do have the console open...?) – JJJ Apr 13 '13 at 09:31
  • http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers and http://stackoverflow.com/questions/4539253/what-is-console-log – JJJ Apr 13 '13 at 09:33
  • i opened console, on running the code, another window opens with message {"error": "Please use POST request"} – navyad Apr 13 '13 at 09:40