简体   繁体   中英

How can I extract address from query string javascript

var str = 'http://maps.google.com/maps?f=d&daddr= 8467 + Perimeter + Rd .+ S + Seattle + Washington + 98108 '

from above string How can i extract address from query string javascript.

8467 Perimeter Rd. S Seattle Washington 98108

I would do something like

var reg = /.*addr=([^&]*)/;
var str = 'http://maps.google.com/maps?f=d&daddr=8467+ Perimeter+ Rd.+S+ Seattle+ Washington+ 98108';
var matches = reg.exec(str);
var address = matches[1].split("+").join(" ");

I made this crunched down function which acts as an equivalent to PHP's $_GET variable:

function _GET(key, def){

        try{
            return RegExp('[?&]'+key+'=([^?&]*)').exec(location.href)[1]
        }catch(err){
            return !def?'':def
        }

    }

this function makes it easily possible to get multiple query strings from the same web address.

//url: 'yoursite.com?f=d&daddr=8467+Perimeter+Rd.+S+Seattle+Washington+98108'

    var SEARCH = _GET('daddr').replace(/[\+]/g, ' ')
    alert(SEARCH)

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