I have a multilingual website (Chinese and English).
I like to validate a text field (name field) in javascript. I have the following code so far.
var c
As of 2018, there is new syntax in JavaScript to match Chinese or any other non-ASCII scripts:
const REGEX = /(\p{Script=Hani})+/gu; // note the 'u'
'你好'.match(REGEX);
// ["你好"]
The trick is to use \p
and use the right script name, Hani
stands for Han script (Chinese). The full list of scripts is here: http://unicode.org/Public/UNIDATA/PropertyValueAliases.txt
To match both Chinese and English you just expand it a bit, for example:
const REGEX = /([A-Za-z]|\p{Script=Hani})+/gu;
// does not match accented letters though