简体   繁体   English

我需要什么正则表达式来检查一些非拉丁字符?

[英]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:只需测试是否存在非 ascii 字符,而不是测试是否存在 ascii 字符:

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使用代码点范围制作自己的正则表达式的另一种方法是使用xregexp 库

Some examples from the documentation:文档中的一些示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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