简体   繁体   中英

What regular expression do I need to check for some non-latin characters?

I am checking a field if it is Latin Characters or not.

var foreignCharacters = $("#foreign_characters").val();
var rlatins = /[\u0000-\u007f]/;

if (rlatins.test(foreignCharacters)) {
  alert("This is Latin Characters");
} else {
  alert("This is non-latin Characters");    
}

This works well, but I would like to change it so when I enter any non-latin characters, such as chinese characters, along with a space(which is within that range I am using currently) it will still say it is non-latin characters.

How can I change the regular expression I have to do that?

Just test for the presence of non-ascii characters instead of testing for the presence of ascii characters:

var foreignCharacters = $("#foreign_characters").val();
var rforeign = /[^\u0000-\u007f]/;

if (rforeign.test(foreignCharacters)) {
  alert("This is non-Latin Characters");
} else {
  alert("This is Latin Characters");    
}

如果要检查整个字符串,请使用(在 php 中)

preg_match('/^[\x{0000}-\x{007F}]*$/u',$s);

An alternative to making your own regex with code point ranges is to use the xregexp library

Some examples from the documentation:

XRegExp('^\\p{Hiragana}+$').test('ひらがな'); // -> true
XRegExp('^[\\p{Latin}\\p{Common}]+$').test('Über Café.'); // -> true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM