【问题标题】:get url uptp required query string jQuery获取 url uptp 所需的查询字符串 jQuery
【发布时间】:2018-03-25 06:21:18
【问题描述】:

考虑我的网址就像 http://localhost:4588/home?name=test&value=2003&view=current&index=1&....

我需要将 url 带到“view”查询字符串。

在 jQuery 中可以吗?

编辑:

url 中的第一个三个值是常量(名称、值和视图),在“视图”之后它会改变。所以我只需要接受“查看”查询字符串。

【问题讨论】:

  • 是的,有可能,请向我们展示您的尝试
  • 想要的输出是:http://localhost:4588/home? name=test&value=2003&view=current?
  • @HamzaAbdaoui 是的

标签: javascript jquery url query-string


【解决方案1】:

您可以使用split()分割网址:

var url = "http://localhost:4588/home?name=test&value=2003&view=current&index=1&....";
console.log(url.split('&index')[0])

说明:

url.split('&index') 将返回具有 2 个值的 array

['http://localhost:4588/home?name=test&value=2003&view=current','=1&....'],你想要的只是第一个值(那个值的索引0 [0])。

更多关于split()

【讨论】:

  • 单独的第一个三个值常量(名称、值和视图).. 在“视图”之后它会改变.. 所以我无法获取索引 [0]。
【解决方案2】:

你可以这样得到:

var str = 'http://localhost:4588/home?name=test&value=2003&view=current&index=1&'

var pattern  = new RegExp(/view=[a-zA-Z0-9]*/); //this is the regex to find view=somevalue (where somevalue will have letters and numbers only

var i = pattern.exec(str); //this extracts the view=current string
console.log('matched view=current -->',i); 
console.log('search for index of view=current in our string',str.indexOf(i[0]));
console.log('length of view=current string-->' + i[0].length);

var final = str.substr(0, str.indexOf(i[0]) + i[0].length); // add the index  + the length of view=current and pass it as second argument to substring function
console.log(final);

【讨论】:

    猜你喜欢
    • 2016-03-27
    • 2011-06-07
    • 2016-12-31
    • 2011-06-05
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多