views:

88

answers:

3

OK, so I'm working with a little regular expression—it's my first time, please, be gentle—and I've run into a problem where I can set a variable with it but if I try to do anything with that variable it causes problems.

Here's my script:

$(document).ready(function () {
    var show_number;
    var url_param;

    $("a[rel=more]").live("click", function(){
     url_param = $(this).attr("href").match(/show:[^/]+/i);
     show_number = url_param.substr(5);

     alert(show_number);

     return false;
    });
});

Now instead of showing the alert the way it should it just follows the link. However, if I get rid of the "show_number" variable and set it to alert the "url_param" everything goes fine.

I basically need to get what comes after the first five characters of that RegEx. I tried adding the substr() function directly to it but that didn't seem to help.

What am I doing wrong?

+3  A: 

match() returns an array, not a string, so at a minimum you need something like this:

url_param = $(this).attr("href").match(/show:[^/]+/i);
if (url_param && url_param.length > 0) {
    show_number = url_param[0].substr(5);
    alert(show_number);
}

A neater way to get the piece of the URL after the "show:" is to use parentheses to capture that part of the URL, which will end up in url_param[1]:

url_param = $(this).attr("href").match(/show:([^/]+)/i);
if (url_param && url_param.length > 0) {
    show_number = url_param[1];
    alert(show_number);
}
RichieHindle
You should also make sure url_param is not null. If the match fails, it returns null, not an empty array. In this case checking for null is sufficient even, because the RegExp requires the captured group.
Matthew Crumley
@Matthew: Good point; now fixed. Thanks!
RichieHindle
A: 

I think the problem is that the JavaScript interpreter cannot parse your code. Your regex:

/show:[^/]+/i

Should be:

/show:[^\/]+/i

because the forward slash is prematurely ending your regex. To verify this, you should view whatever JavaScript error console your browser provides to see if you're getting a JavaScript error.

Jacob
@Jacob: It works fine for me as it is (which I believe is correct, because a forward slash within a character class shouldn't terminate the regex) but a backslash can't hurt.
RichieHindle
A: 

No, the problem is not with the backslash. Jacob is correct, it does not need quoting.

The problem is that indeed, match returns an array. Hence the following simple test case does NOT work.

<html>
<script type="text/javascript">
var a='show: something';
url_param = a.match(/show:[^/]+/i);
show_number = url_param.substring(5);
alert (show_number);
</script>
</html>

However, this second version DOES work.

<html>
<script type="text/javascript">
var a='show: something';
url_param = a.match(/show:[^/]+/i);
show_number = url_param[0].substring(5);
alert (show_number);
</script>
</html>

Even better is probably something explicit which handles whitespace, checks for start and end of string, and does a pattern match extraction.

<html>
<script type="text/javascript">
var a='show: something';
url_param = a.match(/^\s*show:\s*([^/]+)\s*$/i);
show_number = url_param[0].substring(5);
alert (show_number);
</script>
</html>

That's a bit more bullet-proof.

the.jxc