How to get the reversed result of the following regex?

Regex: /^[0-9\p{L}.,\s]+$/u

I would like to replace the characters not matching with the regex with "".

>Solution :

As I understand, you simply want to drop all chars not matching your regex. So the idea is to invert the class of chars:

/^[0-9\p{L}.,\s]+$/u should become /[^\d\p{L}.,\s]+/gu (I added the ^ after the [ to say "not in this list of chars" and replaced 0-9 by \d for digits. Use the g modifier (=global ) to match multiple times.

Running it:

I’m not sure that ,, . and the space will be enough ponctuation. It would be interesting to have a complete example of what you are trying to achieve. You could use another unicode character class for ponctuation if needed, typically with \p{P}. See more info about unicode classes here:

Leave a Reply