In the following function, why replace \[ to \\\[? function getParam(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
name.replace(/[\[]/, "\\\[")
why???
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return results[1];
}
name.replace(/[\[]/, "\\\[")
why???
In fact, this line of code just replaces char '[' into "\\\[", and char ']' is similar with it.
Just by the function name, I guess it might get parameters from a string such as JsonBut "var results = regex.exec(window.location.href); " indicates that get parameter from current url by regexS expression.So why did it like that? There's nobody knows from your code.
var name = "username";
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
document.write("regex: " + regex + "<br />");
var results = regex.exec("http://www.test.net/testpage.aspx?username=nametest&user[id=id001");
if (results == null)
document.write("no match");
else
document.write("result: " + results[1]);
</script>
/*-----output------
regex: /[\?&]username=([^&#]*)/
result: nametest
*/but if "name" include "[" or "]", what will happen?<script type="text/javascript">
var name = "user[id";
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
document.write("regex: " + regex + "<br />");
var results = regex.exec("http://www.test.net/testpage.aspx?username=nametest&user[id=id001");
if (results == null)
document.write("no match");
else
document.write("result: " + results[1]);
</script>no output, only a script error such as "错误: unmatched ) in regular expression", why?
the regex is
/[\?&]a[b=([^&#]*)/
the last "(" is only an element of "[...]", so the last ")" can not match "("
then, "[" or "]" must be escaped when used within the variable "name" <script type="text/javascript">
var name = "user[id";
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
document.write("regex: " + regexS + "<br />");
var results = regex.exec("http://www.test.net/testpage.aspx?username=nametest&user[id=id001");
if (results == null)
document.write("no match");
else
document.write("result: " + results[1]);
</script>
/*-----output------
regex: [\?&]user\[id=([^&#]*)
result: id001
*/
new RegExp这个方法的构造函数需要加\才能表示 转义字符,
另外下面这个转义才完整安全,而不是像上面那样只处理一个字符而已 // accepts a string; returns the string with regex metacharacters escaped. the returned string
// can safely be used at any point within a regex to match the provided literal string. escaped
// characters are [, ], {, }, (, ), -, *, +, ?, ., \, ^, $, |, #, <comma>, and whitespace
var escape = function (str) {
return str.replace(/[-[\]{}()*+?.\\^$|,#\s]/g, "\\$&");
}