I am trying to select just what comes after name=
and before the &
in :
我试着选择name=后面和& in之前的内容:
"/pages/new?name=J&return_url=/page/new"
So far I have..
到目前为止我有. .
^name=(.*?).
I am trying to return in this case, just the J
, but its dynamic so it could very several characters, letters, or numbers.
在这种情况下,我试图返回J,但是它是动态的,所以它可以有很多字符,字母,或数字。
The end case situation would be allowing myself to do a replace
statement on this dynamic variable found by regex.
最终情况是允许我对regex找到的这个动态变量执行替换语句。
6 个解决方案
#1
39
/name=([^&]*)/
/ name =(^ & *)/
- remove the
^
and end with an&
- 删除与一个& ^和结束
Example:
例子:
var str = "/pages/new?name=J&return_url=/page/new";
var matches = str.match(/name=([^&]*)/);
alert(matches[1]);
The better way is to break all the params down (Example using current address):
更好的方法是打破所有的参数(例如使用当前地址):
function getParams (str) {
var queryString = str || window.location.search || '';
var keyValPairs = [];
var params = {};
queryString = queryString.replace(/.*?\?/,"");
if (queryString.length)
{
keyValPairs = queryString.split('&');
for (pairNum in keyValPairs)
{
var key = keyValPairs[pairNum].split('=')[0];
if (!key.length) continue;
if (typeof params[key] === 'undefined')
params[key] = [];
params[key].push(keyValPairs[pairNum].split('=')[1]);
}
}
return params;
}
var url = "/pages/new?name=L&return_url=/page/new";
var params = getParams(url);
params['name'];
Update
Though still not supported in any version of IE, URLSearchParams provides a native way of retrieving values for other browsers.
虽然在任何版本的IE中仍然不支持URLSearchParams,但是它为其他浏览器提供了一种获取值的本地方法。
#2
6
Improving on previous answers:
改善之前的答案:
/**
*
* @param {string} name
* @returns {string|null}
*/
function getQueryParam(name) {
var q = window.location.search.match(new RegExp('[?&]' + name + '=([^&#]*)'));
return q && q[1];
}
getQueryParam('a'); // returns '1' on page http://domain.com/page.html?a=1&b=2
#3
4
The accepted answer includes the hash part if there is a hash right after the params. As @bishoy has in his function, the correct regex would be
接受的答案包括哈希部分,如果在解析之后有哈希部分。正如@bishoy在其函数中所说,正确的regex将是
/name=([^&#]*)/
#4
3
The following should work:
以下工作:
\?name=(.*?)&
#5
3
here is the full function (tested and fixed for upper/lower case)
这是完整的函数(测试和固定的大小写)
function getParameterByName (name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name.toLowerCase() + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search.toLowerCase());
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
#6
1
var myname = str.match(/\?name=([^&]+)&/)[1];
The [1] is because you apparently want the value of the group (the part of the regex in brackets).
[1]是因为您显然需要组的值(方括号中regex的部分)。
var str = "/pages/new?name=reaojr&return_url=/page/new";
var matchobj = str.match(/\?name=([^&]+)&/)[1];
document.writeln(matchobj); // prints 'reaojr'
#1
39
/name=([^&]*)/
/ name =(^ & *)/
- remove the
^
and end with an&
- 删除与一个& ^和结束
Example:
例子:
var str = "/pages/new?name=J&return_url=/page/new";
var matches = str.match(/name=([^&]*)/);
alert(matches[1]);
The better way is to break all the params down (Example using current address):
更好的方法是打破所有的参数(例如使用当前地址):
function getParams (str) {
var queryString = str || window.location.search || '';
var keyValPairs = [];
var params = {};
queryString = queryString.replace(/.*?\?/,"");
if (queryString.length)
{
keyValPairs = queryString.split('&');
for (pairNum in keyValPairs)
{
var key = keyValPairs[pairNum].split('=')[0];
if (!key.length) continue;
if (typeof params[key] === 'undefined')
params[key] = [];
params[key].push(keyValPairs[pairNum].split('=')[1]);
}
}
return params;
}
var url = "/pages/new?name=L&return_url=/page/new";
var params = getParams(url);
params['name'];
Update
Though still not supported in any version of IE, URLSearchParams provides a native way of retrieving values for other browsers.
虽然在任何版本的IE中仍然不支持URLSearchParams,但是它为其他浏览器提供了一种获取值的本地方法。
#2
6
Improving on previous answers:
改善之前的答案:
/**
*
* @param {string} name
* @returns {string|null}
*/
function getQueryParam(name) {
var q = window.location.search.match(new RegExp('[?&]' + name + '=([^&#]*)'));
return q && q[1];
}
getQueryParam('a'); // returns '1' on page http://domain.com/page.html?a=1&b=2
#3
4
The accepted answer includes the hash part if there is a hash right after the params. As @bishoy has in his function, the correct regex would be
接受的答案包括哈希部分,如果在解析之后有哈希部分。正如@bishoy在其函数中所说,正确的regex将是
/name=([^&#]*)/
#4
3
The following should work:
以下工作:
\?name=(.*?)&
#5
3
here is the full function (tested and fixed for upper/lower case)
这是完整的函数(测试和固定的大小写)
function getParameterByName (name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name.toLowerCase() + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search.toLowerCase());
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
#6
1
var myname = str.match(/\?name=([^&]+)&/)[1];
The [1] is because you apparently want the value of the group (the part of the regex in brackets).
[1]是因为您显然需要组的值(方括号中regex的部分)。
var str = "/pages/new?name=reaojr&return_url=/page/new";
var matchobj = str.match(/\?name=([^&]+)&/)[1];
document.writeln(matchobj); // prints 'reaojr'