检查url字符串中的重复val

时间:2015-09-02 13:51:48

标签: javascript underscore.js lodash

我是url字符串,我想知道它是否包含重复的端口多一次,例如在这种情况下54001或77777存在多次? 我已经破解并强调了js

http://gr-t6fa45e73.go.grp.corp:54001/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Fro-d3ma32e85.mo.grp.corp%3A77777%2Flogin%2Fcallback&client_id=zs6.node&test=54001

1 个答案:

答案 0 :(得分:2)

这样的东西?这个纯JS函数试图搜索与url ports匹配的url params

function search() {
    var url = unescape(document.getElementById("url").innerHTML); 
    var patternPort = /:\d+/g;    
    var ports = url.match(patternPort);

    for (i=0; i<ports.length; i++){
       var patternParam= new RegExp("=" + ports[i].substr(1), 'g');
       if(url.match(patternParam)){
          alert(ports[i].substr(1) + " is repeated");
       }
    }        
}
<pre id="url">http://gr-t6fa45e73.go.grp.corp:54001/oauth/authorize?response_type=code&redirect_uri=http%3A%2F%2Fro-d3ma32e85.mo.grp.corp%3A77777%2Flogin%2Fcallback&client_id=zs6.node&test=54001</pre>
<button onclick="search()">Search repeated ports on params</button>