[正则表达式]匹配Unicode

一、PHP[PCRE]之Unicode

PCRE支持的16进制字符编码转义符有

x00-xFF,或x{num},num为任意位16进制数

但并不支持u0000-uFFFF这的形式

PCRE运用/u模式去处理UTF-8编码字符,这是PCRE特有的,示例代码

$str = '中asfdsf个业上';
$pattern = '/[x{4E10}-x{4E2F}x{4E0A}]/u';
$matchs = array();
$result = preg_match_all($pattern, $str, $matchs);
var_dump($result, $matchs);

示例中“中个业上”将会被匹配,因为字符组中x{4E10}-x{4E2F}匹配了“中个业”所在的Unicode区间,x{4E0A}对应的是“上”;

二、JavaScript

JavaScript支持的16进制字符编码转义符有

x00-xFF, u0000-uFFFF

示例代码

var str = "中asfdsf个业上";
var regExp = /[u4E10-u4E2Fu4E0A]/g;
console.log(str.match(regExp));

示例中“中个业上”将会被匹配,原因同上;

原文地址:https://www.cnblogs.com/yiyide266/p/9278882.html