Remove special characters from a string except whitespace

前端 未结 2 1456
既然无缘
既然无缘 2021-01-14 07:05

I am looking for a regular expression to remove all special characters from a string, except whitespace. And maybe replace all multi- whitespaces with a single whitespace.

相关标签:
2条回答
  • 2021-01-14 07:48
    [ ](?=[ ])|[^-_,A-Za-z0-9 ]+
    

    Try this.See demo.Replace by empty string.See demo.

    http://regex101.com/r/lZ5mN8/69

    0 讨论(0)
  • 2021-01-14 08:06

    Use the regex [^\w\s] to remove all special characters other than words and white spaces, then replace:

    Regex.Replace("[one@ !two three-four]", "[^\w\s]", "").Replace("  ", " ").Trim
    
    0 讨论(0)
提交回复
热议问题