How to remove everything but:
letters, numbers, spaces, exclamation marks, question marks from a string?
It’s important that the method supports international languages (UTF-8).
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.
You can use regex
This will replace everything but a word character, space, exclamation mark, or question.
If you don’t want the underscore, you can use just
[A-Za-z0-9].For unicode characters, you can add something like
\u0000-\u0080to the expression. That will exclude all characters within that unicode range. You’ll have to specify the range for the characters you don’t want removed. You can see all the codes on Unicode Map. Just add in the characters you want kept or a range of characters.For example:
This will allow all the previously mentioned characters, the range from
\u0000-\u0080and\u0082. It will remove\u0081.