I want to remove non-alphanumeric character in a string, but not remove international characters, like accented letters. I also want to keep whitespace. Here is what I have so far:
the_string = the_string.gsub(/[^a-z0-9 -]/i, '')
This does remove international accented alpha characters though.
Solution that I used:
the_string = the_string.gsub(/[^\p{Alnum}\p{Space}-]/u, '')
It works! Thanks.
You can use character properties to do this:
the_string.gsub(/[^\p{Alnum} -]/, '')
You may also want to use \p{Space}
to keep other whitespace such as non-breaking spaces etc.:
the_string.gsub(/[^\p{Alnum}\p{Space}-]/, '')
(This also keeps the -
character, which you have in your regexp.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With