Regex: find whatever comes after one thing before another thing

后端 未结 4 562
南笙
南笙 2021-01-15 17:55

I want to find anything that comes after s= and before & or the end of the string. For example, if the string is

t=qwert

4条回答
  •  一整个雨季
    2021-01-15 18:19

    Why don't you try something that was generically aimed at parsing query strings? That way, you can assume you won't run into the obvious next hurdle while reinventing the wheel.

    jQuery has the query object for that (see JavaScript query string)

    Or you can google a bit:

    function getQuerystring(key, default_)
    {
       if (default_==null) default_=""; 
       key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
       var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
       var qs = regex.exec(window.location.href);
       if(qs == null)
         return default_;
       else
         return qs[1];
    }
    

    looks useful; for example with

    http://www.bloggingdeveloper.com?author=bloggingdeveloper
    

    you want to get the "author" querystring's value:

    var author_value = getQuerystring('author');
    

提交回复
热议问题