I have a code snippet which parses the url and returns some information after the occurrence of ? in the url:
If the function is called as getValue("params"); then the entire url is parsed until ?params= is found and what ever follows after that is returned.Code:function getValue(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]; }
I can also see that if the function is called as getValue("[params]"); then the line:
makes name as \[params\]Code:name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
Now my question is
1. What significance does [ or ] character have in JS. Why are they escaped?
2. What is being achieved by the line var regexS = "[\\?&]"+name+"=([^&#]*)";
why are we appending so many characters to name?


Reply With Quote
Bookmarks