Fix for escaping str literals in JS regexes

This PR is potential fix for issues #507, #575 and #1054. It addresses JS errors related to the use of unescaped string literals inside keywords, and it is based in a previous PR (#509) by user @jrfnl. It's been successfully tested by the OP of #507, @blafrance.
Solution based on SO thread: http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex