2011-04-12 78 views
1

我不是一个JS大师,但有人可以帮我找到下面的代码片段中的无效量词错误吗?Javascript - 无效的量词错误,有人能帮我看看我的错误吗?

感谢提前! -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; 
    } 
+0

这是应该做的:'/ ^?/'?它给了我* SyntaxError:无效的正则表达式:/ ^?/:没有重复*。也许你也可以解释功能应该做什么。 – 2011-04-12 16:46:16

+1

您需要转义?或将其从搜索字符串中删除-s = s || (1)window.location.search.substring; – kennebec 2011-04-12 16:50:34

+0

这个函数应该像var var1 = $ _GET('key')一样在javascript中使用get var – none 2011-04-12 17:48:26

回答

1

?在正则表达式特殊的意义,特别是它使前面的项可选。如果您正在尝试查找问号字符本身,则需要使用反斜杠进行转义。

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; 
    } 
+0

我会试一试,谢谢输入! – none 2011-04-12 17:53:24

+0

成功,你是男人 – none 2011-04-12 18:54:29

相关问题