Escape characters prior to using Selector

Escape characters prior to using Selector


I'm trying to escape characters within a selector as follows:

  1. var findString = "[value='" + str.replace(/([ #;&,.%+*~\':"!^$[\]()=>|\/])/g,'\\$1') + "']";
  2. someElement.find(findString);
When str contains an apostrophe (for example, str=John's), the find fails with the following error in Firefox

uncaught exception: Syntax error, unrecognized expression: value='John\'s']


I've seen a few post surrounding this issue, but the fix seems to escape the characters as I have done.

Any thoughts on why this isn't working? Thanks in advance for your help.