Home  >  Q&A  >  body text

javascript - Ask a question about using regular expressions to obtain URL parameters

var url = "https://api.xxx.com/search?name=xxx&age=xxx&sex=xxx";

Given this url, match the parameter string following ? (not including?) through a regular expression. Please tell me how to write the regular expression. Xiaobai asks for advice~~

天蓬老师天蓬老师2710 days ago925

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-06-12 09:31:39

    Because js does not support forward lookahead syntax, it cannot use the syntax (?<=?), so we can only save the country through curves:

    var url = "https://api.xxx.com/search?name=xxx&age=xxx&sex=xxx";
    var reg = /\?[^"/]+/
    var result = reg.exec(url)
    para = result[0].slice(1)
    console.log(para)
    
    // 输出:
    name=xxx&age=xxx&sex=xxx

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:31:39

    This does not require regularization.

    pos = str.indexOf('?')

    str.substring(pos+1)

    reply
    0
  • Cancelreply