I was trying to replace this (' with (\' and ') with \') My attempts were few as I’m not comfortable with RegExp.
What I tried was myText.replace(/\(\'/g, "(\')"); and myText.replace(/\'\)/g, "\')");
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.
The problem is not your regex but your replacement. You need to escape the backslash, otherwise it (unnecessarily) escapes the
':With some slightly more advanced techniques (namely a capturing group and a lookahead) you can combine these into one regex:
The regex matches either
('or'if it is followed by)(without actually matching that). At the same time we “capture” the(if it is there. The$1in the replacement string puts the(back in place if it was captured, and then simply writes out\'. There is no need to write the)back. Because we used a lookahead, it was never part of the match.