Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to reject all non-English characters except some characters with accents

This works great to disallow all non-English letters:

/[^\x00-\x7F]+/

But I would like to allow these characters:

âäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ

How do I add those to the regex so that they are allowed?

like image 800
pk1557 Avatar asked Oct 19 '25 13:10

pk1557


1 Answers

If the pattern like /[^\x00-\x7F]+/ works for you, it matches all the letters you now want to avoid matching.

Since the [^...] is a negated character class, the easiest way to exclude a char/set of chars is to just add them to the class:

/[^\x00-\x7FâäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇ]+/

See the regex demo.

If you use an empty string as the replacement pattern, you will remove every 1+ chars that are not ASCII (\x00-\x7F) and that are not equal to the letters added to the negated character class.

like image 107
Wiktor Stribiżew Avatar answered Oct 22 '25 02:10

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!