In HTML, tags and entities aren\'t parsed within tags, and immediately ends the tag. Thus,
I came here looking for a way to universally escape inside the JavaScript code.
After bit of research I figured that if you are trying to escape in JavaScript code so it can be safely embedded in html between and tags you should replace with or . It's safer to do because if you replace it with <\/script you might break JavaScript code like this: var q = -1
Be careful not to look for but rather because will end your script just as well as does.
Sorry, it doesn't help OP in any way. Accepted answer is absolutely correct that you need to know what constructs are legal in language you have inside your to know how to escape occurrence without braking the code.