Skip to content

Instantly share code, notes, and snippets.

@crazymykl
Created June 7, 2012 23:43
Show Gist options
  • Save crazymykl/2892439 to your computer and use it in GitHub Desktop.
Save crazymykl/2892439 to your computer and use it in GitHub Desktop.
A quickie to escape metacharacters in strings for use in jQuery selectors
def jqe(str)
str.gsub( /[!"\#$%&'()*+,.\/:\\;<=>?@\[\]^`{|}~]/, '\\\\\\\\\&')
end
@crazymykl
Copy link
Author

to make this nil-safe, call .to_s on str first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment