使用jquery解析值并将解析值附加到URL的末尾

时间:2014-04-25 20:16:00

标签: jquery regex

我需要一个解决方案来解析%2526_ga%253d&wct之间的网址拆分值,然后将&_ga= + the parsed value添加到同一网址的末尾。

这是我的网址,它始终不同,但%2526_ga%253d&wct始终相同。 http://www.example.com/wa=wsignin1.0%2526_ga%253d1.193373933.1506621463.1391436765&wct=2014-04-25T19

最终结果将是: http://www.example.com/wa=wsignin1.0%2526_ga%253d1.193373933.1506621463.1391436765&wct=2014-04-25T19 &_ga=1.193373933.1506621463.1391436765

我完成了我的解决方案,这有效:

var gacookie = window.location.search.match('_ga%253d(.+)&wct=');
var url = window.location.href;

if (url.indexOf('_ga') > -1) {
        url += '&_ga=' + gacookie[1]
        parent.location.hash = url
        var hash = location.hash.replace('#', gacookie[1]); 
        if(hash != '') {
        location.hash = '&_ga=' + gacookie[1];
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用RegEx执行此操作:

var url = 'http://www.example.com/wa=wsignin1.0%2526_ga%253d1.193373933.1506621463.1391436765&wct=2014-04-25T19';
var value = url.match('_ga%(.+)&wct=');
url = url + "&_ga=" + value[1];

你也应该添加一些验证逻辑......

答案 1 :(得分:0)

这是完成的结果。感谢@Mattias帮助我完成比赛。

var gacookie = window.location.search.match('_ga%253d(.+)&wct=');
var url = window.location.href;

if (url.indexOf('_ga') > -1) {
        url += '&_ga=' + gacookie[1]
        parent.location.hash = url
        var hash = location.hash.replace('#', gacookie[1]); 
        if(hash != '') {
        location.hash = '&_ga=' + gacookie[1];
    }
}