0

How would I write an if statement that alerts when the var MyString is not in the var URL?

  var URL = "http://test.mysite.com/about/";
  var MyString = "mysite.com";
user2238083
  • 583
  • 2
  • 9
  • 21

2 Answers2

4
if (URL.indexOf(MyString) === -1) {
    alert('Not found!');
}
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
0
if(URL.indexOf(MyString) == -1) alert("Not found");
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • I have two notes: 1) treating string literal as regular expression is not the same as checking if string is found withing a string (see especially special characters in pattern string), 2) the comparisons you are using are _evil_ (see more here: http://stackoverflow.com/a/359509/548696). Downvoting because of these two reasons. – Tadeck Aug 06 '13 at 00:11
  • 1
    @Tadeck Your second point is a personal style preference (even though it's same as mine). – sabof Aug 06 '13 at 00:29
  • @Tadeck - You are right I should probably remove the regex one. – Derek 朕會功夫 Aug 06 '13 at 00:41