How to avoid all special characters except spaces in a regex?

13,062

Just add space in your negation character class to skip space to be replaced by a comma.

Regex.Replace(currentText, "[^0-9A-Za-z ,]", ",");

PS: I added comma also in your character class to avoid comma getting replaced by comma.

Share:
13,062
Kannan
Author by

Kannan

Updated on June 08, 2022

Comments

  • Kannan
    Kannan almost 2 years

    I am building, AutoComplete editor by using Row Filter and i am using Regex to remove special characters. But spaces are also removed.

    Regex :

    Regex.Replace(currentText, "[^0-9A-Za-z]", ",");
    

    I just want to ignore spaces but replace the remaining special characters.