Search code examples
javascriptregexnon-english

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?


Solution

  • 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.