I want to remove characters &%*@()!{} from a string. I tried this code:
keyword.gsub!(/[\&\%\*\@\(\)\!\{\}]/, '')`
but it failed.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your regex isn’t right. It appears you don’t understand how
[...]works in a regex.You can use:
For instance:
An alternate way to do this, without using a regex is to use the
trmethod:The benefit of using
tr, AKA “translate” is it doesn’t take a regex, and can do deletions, like here, or translations from one character to another. It’s also very fast.Returns on my machine running 1.9.3-p362: