从Javascript中的变量中提取内容

时间:2014-07-11 09:30:33

标签: jquery

我在javascript中有这个变量,任何人都可以让我知道,我如何解析orderIdscreen_id ??

 var data =   'http://localhost:8080/OrderSnacks/oms/vendorsplit?orderId='1234'&screen_ids=3';

我试过这种方式

var data =   'http://localhost:8080/OrderSnacks/oms/vendorsplit?orderId='1234'&screen_ids=3';

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}


var result = getParameterByName('orderId');

alert(result);

1 个答案:

答案 0 :(得分:0)

也许是这样的:

var url = 'http://localhost:8080/OrderSnacks/oms/vendorsplit?orderId=1234&screen_ids=3';
var qs = parseQueryString(url);

function parseQueryString(url){
    var dict = [];
    var qs = url.split('?');
    if(qs.length > 1){
       var params = qs[1].split('&');   
       for(var i = 0 ;i < params.length ; i++){
           var curr = params[i].split('=');
           dict.push({key: curr[0], value: curr[1]});
      }
  }
  return dict;

}

这是JSFiddle