Remove specific character if at the beginning of a string or consecutive at any place in a string

前端 未结 2 938
囚心锁ツ
囚心锁ツ 2021-01-25 05:17

I\'m currently trying to make a regex to remove every of those character [0-9] \\ - * \\\' if they are either at the beginning of the string, end of the string or if they are co

2条回答
  •  情书的邮戳
    2021-01-25 06:13

    You might use an alternation and a character class listing all the characters that you want to remove at the start of the string, the end or repeated 2 or more times using {2,}

    ^[ *'0-9?&_$-]+|[ *'0-9?&_$-]+$|[ *'0-9?&_$-]{2,}
    

    Regex demo

    If you want to remove all except characters a-zA-Z and a negated character class to match any character not in the character class

    In the replacement use an empty string.

    ^[^a-zA-Z]+|[^a-zA-Z]+$|[^a-zA-Z]{2,}
    

    Regex demo

提交回复
热议问题