Javascript - invalid quantifier error, can someone help me see my mistake?
I'm not a 开发者_JS百科JS guru, but could someone help me find the invalid quantifier error in the following snippet?
THANKS IN ADVANCE! -mprototype
function $_GET(q,s) {
s = s ? s : window.location.search;
var re = new RegExp( '&' + q + '(?:=([^&]*))?(?=&|$)' , 'i' );
return (s=s.replace(/^?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
?
has a special meaning in Regular Expressions, specifically it makes the preceding item optional. If you are trying to find the question mark character itself you need to escape it with a backslash.
function $_GET(q,s) {
s = s ? s : window.location.search;
var re = new RegExp( '&' + q + '(?:=([^&]*))?(?=&|$)' , 'i' );
return (s=s.replace(/^\?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined;
}
精彩评论