I use this function to escape jquery selectors. It escapes basically everything questionable but may be too aggressive.
function escapeStr(str)
{
if (str)
return str.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1');
return str;
}