Search code examples
c#regexchars

Unable to remove invisible chars using Regex


I want to remove any invisible chars from a string, only keep spaces & any chars from 0x20-0x7F, I use this: Regex.Replace(QueryString, @"[^\s\x20-\x7F]", ""); However it does not work

QueryString has a char 0xA0, after that, the char still exists in QueryString.

I am not sure why this failed to work?


Solution

  • 0xA0 is the non-breaking space character - and as such it's matched with \s. Rather than using \s, expand this out into the list of whitespace characters you want to include.