开发者

Why does this jQuery code not work?

Why doesn't the following jQuery开发者_如何学JAVA code work?

$(function() {
    var regex = /\?fb=[0-9]+/g;
    var input = window.location.href;

    var scrape = input.match(regex); // returns ?fb=4

    var numeral = /\?fb=/g;

    scrape.replace(numeral,'');
    alert(scrape); // Should alert the number?
});

Basically I have a link like this:

http://foo.com/?fb=4

How do I first locate the ?fb=4 and then retrieve the number only?


Consider using the following code instead:

$(function() {
    var matches = window.location.href.match(/\?fb=([0-9]+)/i);

    if (matches) {
        var number = matches[1];
        alert(number); // will alert 4!
    }
});

Test an example of it here: http://jsfiddle.net/GLAXS/

The regular expression is only slightly modified from what you provided. The global flag was removed, as you're not going to have multiple fb='s to match (otherwise your URL will be invalid!). The case insensitive flag flag was added to match FB= as well as fb=.

The number is wrapped in curly brackets to denote a capturing group which is the magic which allows us to use match.

If match matches the regular expression we specify, it'll return the matched string in the first array element. The remaining elements contain the value of each capturing group we define.

In our running example, the string "?fb=4" is matched and so is the first value of the returned array. The only capturing group we have defined is the number matcher; which is why 4 is contained in the second element.


If you all you need is to grab the value of fb, just use capturing parenthesis:

    var regex = /\?fb=([0-9]+)/g; 
    var input = window.location.href;

    var tokens = regex.exec(input);

    if (tokens) { // there's a match
        alert(tokens[1]); // grab first captured token
    }


So, you want to feed a querystring and then get its value based on parameters?


I had had half a mind to offer Get query string values in JavaScript.

But then I saw a small kid abusing a much respectful Stack Overflow answer.

// Revised, cooler.
function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);
    return match ?
        decodeURIComponent(match[1].replace(/\+/g, ' '))
        : null;
}

And while you are at it, just call the function like this.

getParameterByName("fb")


How about using the following function to read the query string parameter in JavaScript:

function getQuerystring(key, default_) {
    if (default_==null)
        default_="";
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null)
        return default_;
    else
        return qs[1];
}

and then:

alert(getQuerystring('fb'));


If you are new to Regex, why not try Program that illustrates the ins and outs of Regular Expressions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜