正则表达式匹配中文|正则表达式验证中文代码

更新时间:2016-10-20    来源:正则表达式    手机版     字体:

【www.bbyears.com--正则表达式】

str = "中华人民共和国123456789abcdefg";
echo preg_match("/^[u4e00-u9fa5_a-za-z0-9]{3,15}",strname);

运行一下上面这段代码,看会有什么提示信息?
warning: preg_match(): compilation failed: pcre does not support l, l, n, p, p, u, u, or x at offset 3 in f:www.111cn.netphp教程test.php on line 2

原来,php正则表达式中不支持下列 perl 转义序列:l, l, n, p, p, u, u, or x

在 utf-8 模式下,允许用"x{...}",花括号中的内容是表示十六进制数字的字符串。原来的十六进制转义序列 xhh 如果其值大于 127 的话则匹配了一个双字节 utf-8 字符。
所以,可以这样来解决preg_match("/^[x80-xff_a-za-z0-9]{3,15}",strname);
方法一
<script>
function ischinese(temp)
{
var re = /[^u4e00-u9fa5]/;
if(re.test(temp)) return false;
return true;
}
alert(ischinese("中文"));
</script>

方法二

function   checkischinese(str)  
{  
//如果值为空,通过校验  
if   (str   ==   "")  
return   true;  
var   pattern   =   /^([u4e00-u9fa5]|[ufe30-uffa0])*$/gi;  
if   (pattern.test(str))  
return   true;  
else  
return   false;  
}//~~~  
/**  
*   计算字符串的长度,一个汉字两个字符  
*/  
string.prototype.reallength   =   function()  
{  
return   this.replace(/[^x00-xff]/g,"**").length;  
}  

本文来源:http://www.bbyears.com/aspjiaocheng/27683.html

猜你感兴趣

热门标签

更多>>

本类排行