Let’s say I have a string that can contain any UTF-16 characters, but I want to replace all characters not in a whitelist with an underscore. Let’s say the whitelist is [A-Za-z], [0-9], and [-:.].
How would I use the Regex class to replace all characters not in the whitelist?
You can do it with this:
The caret is the negation operator. So this will match every character that’s not in the character class.
And then you simply replace the matches with an underscore like this:
Here it is in action.