consider you are searching for Cs that are appeared after a Bs.
Why following code returns -1 instead of 2:
console.log('abc'.search(/(?=b)c/));
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.
Because
(?=is for lookahead, not lookbehind.And before you ask: JavaScript regex does not support lookbehind. In this simple case, however, you can use a workaround based on lookahead:
This works because you’re looking for the position of
cpreceded byb, but this is logically the same as one greater than the position ofbfollowed byc.Before you get too excited, however: you do not need regex for this. At all.