Question

I'm trying to build a bookmarklet that preforms a service client side, but I'm not really fluent in Javascript. In my code below I want to take the current page url and first verify that it's a url following a specific format after the domain, which is...

/photos/[any alphanumeric string]/[any numeric string]

after that 3rd "/" should always be the numeric string that I need to extract into a var. Also, I can't just start from the end and work backwards because there will be times that there is another "/" after the numeric string followed by other things I don't need.

  1. Is indexOf() the right function to verify if the url is the specific format and how would I write that expression? I've tried several things related to indexOf() and Regex(), but had no success. I seem to always end up with an unexpected character or it just doesn't work.

  2. And of course the second part of my question is once I know the url is the right format, how do I extract the numeric string into a variable?

Thank you for any help!

javascript:(function(){

// Retrieve the url of the current page
var photoUrl = window.location.pathname;

if(photoUrl.indexOf(/photos/[any alphanumeric string]/[any numeric string]) == true) {
    // Extract the numeric substring into a var and do something with it
} else {
    // Do something else
}

})();
Was it helpful?

Solution

var id = window.location.pathname.match(/\/photos\/(\w+)\/(\d+)/i); 
if (id) alert(id[1]); // use 1 or 2 depending on what you want 
else alert('url did not fit expected format');

(EDIT: changed first \d* to \w+ and second \d* to \d+ and dig to id.)

OTHER TIPS

To test strings for patterns and get their parts, you can use regular expressions. Exression for your criteria would be like this:

/^\/photos\/\w+\/(\d+)\/?$/

It will match any string starting with /photos/, followed by any alphanumeric character (and underscore), followed by any number and optional / at the end of string, wrapped in a capture group.

So, if we do this:

"/photos/abc123/123".match(/^\/photos\/\w+\/(\d+)\/?$/)

the result will be ["/photos/abc123/123", "123"]. As you might have noticed, capture group is the second array element.

Ready to use function:

var extractNumeric = function (string) {
    var exp = /^\/photos\/\w+\/(\d+)\/?$/,
        out = string.match(exp);

    return out ? out[1] : false;
};

You can find more detailed example here.

So, the answers:

Is indexOf() the right function to verify if the url is the specific format and how would I write that expression? I've tried several things related to indexOf() and Regex(), but had no success. I seem to always end up with an unexpected character or it just doesn't work.

indexOf isn't the best choice for the job, you were right about using regular expression, but lacked experience to do so.

And of course the second part of my question is once I know the url is the right format, how do I extract the numeric string into a variable?

Regular expression together with match function will allow to test string for desired format and get it's portions at the same time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top