views:

89

answers:

1

How do I skip over this next line if it turns out to be null? Currently, it (sometimes) "breaks" and prevents the script from continuing.

var title = (/(.*?)<\/title>/m).exec(response)[1];

$.get(url, function(response){
    var title = (/<title>(.*?)<\/title>/m).exec(response)[1];
    if (title == null || title == undefined){
        return false;
    }
    var words = title.split(' ');
    $.each(words, function(index, value){
        $link.highlight(value + " ");
        $link.highlight(" " + value);
    });
 });        
+3  A: 
$.get(url, function(response){
    var title = (/<title>(.*?)<\/title>/m).exec(response);
    if (!title || !title[1]){
        return false;
    }
    title=title[1];
    var words = title.split(' ');
    $.each(words, function(index, value){
        $link.highlight(value + " ");
        $link.highlight(" " + value);
    });
 });

You must check that title is not null before you get the result at index 1

mck89
`if (!title)` would be just fine too, since exec will either return a match or `null`.
Andy E
Yeah right, i've updated it
mck89
You must check that title`s length is greater than 1
chapluck
@chapluck: he could just change it to `if (!title || !title[1])` if he wanted to return on an empty title string (`<title></title>`)
Andy E