dougoftheabaci
dougoftheabaci

Reputation:

My RegEx is screwing up the rest of my javascript

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?

Upvotes: 1

Views: 177

Answers (3)

RichieHindle
RichieHindle

Reputation: 281415

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);
}

Upvotes: 3

the.jxc
the.jxc

Reputation: 3461

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.

Upvotes: 0

Jacob
Jacob

Reputation: 78850

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.

Upvotes: 0

Related Questions