Could you please help me understand this javascript RegExp :
cbreg = new RegExp('((^|\\?|&)' + cbkey + ')=([^&]+)')
// where cbkey is a string
I am confused by the (^|\\?|&) portion. What could that mean?
Thanks !
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well first of all given that the regex is created from a string literal the double backslashes become only a single backslash in the resulting regex (because that’s how escaping works in a string literal):
The
|means OR, so then you have:A question mark on its own has special meaning within a regex, but an escaped question mark matches an actual question mark.
The parentheses means it matches one of those choices before matching the next part of the regex. Without parens the third choice would include the next part of the expression (whatever is in
cbkey).