简体   繁体   中英

Get specific part of url

I have a set of urls that i need to get a specific part of . The format of the url is :

http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg

I need to get the 1234567 bit and store that in a var.

Well you can do splits

"http://xxx.xxxxx.com/xxxx/xxxx/1234567_1.jpg".split("/").pop().split("_").shift()

or a regular expression

"http://xxx.xxxxx.com/xxxx/xxxx/1234567_1.jpg".match(/\/(\d+)_\d+\.jpg$/).pop()

You should be able to get it to work with your JSON string by checking the URL with a function. Something like this should work:

function checkForMatches(str) {
    var res = str.match(/.*\/(.*)_1.jpg/);    
    if(res) {
        output = res[res.length-1];
    } else {
        output = false;
    }    
    return output;      
}


$.get("test.php", function (data) {
    // now you can work with `data`
    var JSON = jQuery.parseJSON(data); // it will be an object
    $.each(JSON.deals.items, function (index, value) {
        //console.log( value.title + ' ' + value.description );
        tr = $('<tr/>');
        tr.append("<td>" + "<img class='dealimg' src='" + value.deal_image + "' >" + "</td>");
        tr.append("<td>" + "<h3>" + value.title + "</h3>" + "<p>" + value.description + "</p>" + "</td>");
        //tr.append("<td>" + value.description + "</td>");
        tr.append("<td> £" + value.price + "</td>");
        tr.append("<td class='temperature'>" + value.temperature + "</td>");
        tr.append("<td>" + "<a href='" + value.deal_link + "' target='_blank'>" + "View Deal</a>" + "</td>");

        myvar = checkForMatches(value.deal_link);
        if(myvar == false) {
            myvar = value.deal_link; //if no matches, use the full link
        }


        tr.append("<td>" + "<a href='" + myvar + "' target='_blank'>" + "Go To Argos</a>" + "</td>");
        $('table').append(tr);
    });
});


Earlier, more basic examples.

You can use a regular expression to find the match.

Something like this would work:

var str = "http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg"; 
var res = str.match(/.*\/(.*)_1.jpg/);
alert(res[1])

If you wanted to go a little further with it, you could create a function and pass the strings you wanted to test, and it would return the matched value if found, or boolean false if no matches exist.

Something like this would work:

function checkForMatches(str) {
    var res = str.match(/.*\/(.*)_1.jpg/);

    if(res) {
        output = res[res.length-1];
    } else {
        output = false;
    }

    return output;

}


alert(checkForMatches("http:\/\/xxx.xxxxx.com\/xxxx\/xxxx\/1234567_1.jpg"))
alert(checkForMatches("this is an invalid string"))

You can see it working here: https://jsfiddle.net/9k5m7cg0/2/

Hope that helps!

var pathArray = window.location.pathname.split( '/' );

to split 1 / 2/ 3/ 4...

So to get path 2 it would be:

var setLocation = pathArray[1];

Well This should do

function getLastFolder(){
    var path = window.location.href;
    var folders =path.split("/");
    return folders[folders.length-1]);
}

Here's the idea: take everything that comes after the final / character, and then take everything within that substring that comes before the first _ character.

var getUrlTerm = function(url) {
    var urlPcs = url.split('/');
    var lastUrlPc = urlPcs[urlPcs.length - 1];
    return lastUrlPc.split('_')[0];
}

You can attribute the url to an 'A' element and use javascript's built in methods to make your life easier:

var parser = document.createElement('a');
parser.href = "YOUR URL HERE";
var fileName = parser.pathname.split('/').pop();
var code = fileName.split('_')[0];

code will have the value you want.

I would use a regular expression and sense it seems you are looking for numbers you can do the regex filter for that.

var path = window.location.pathname,
      regFilter = /\d*/g,
      filter = regFilter.exec(path);

The regular expression \\d narrows your filter search to only look for digits. And the * grabs the group of digits.

Your result is in the filter var. The only thing about this is that the exec returns an array with your original string and the returned result which will be at the 1 index so you'll have to grab it from there like so.

filter[1];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM