如何实现用正则表达式判断输个入的姓名是不是合法(中文,2-7个汉字)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何实现用正则表达式判断输个入的姓名是不是合法(中文,2-7个汉字)相关的知识,希望对你有一定的参考价值。
如题
function IsChinese()var str = document.all.realname.value;
reg=/^([\u4e00-\u9fa5])2,7$/; //只能是中文,长度为2-7位
if(!reg.test(str))
alert("对不起,您输入的字符串类型格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称!
return false;
参考技术A 如果是GB码,可能是双字节GBK[0x81-0xfe][0x40-0xfe],也可能是四字节GB18030[0x81-0xfe][0x30-0x39][0x81-0xfe][0x30-0x39]。
汉字在Unicode中称作CJK 统一表意符号 (CJK Unified Ideographs),包括
reg=/^([\u+3400-\u+4dbf\u4e00-\u9fff\u+20000-\u3fffd\ue000-f8ff]2,7)$/; //只能是中文,长度为2-7位
最后一项为PUA,参见百度百科【统一码】。 参考技术B 呵呵,我也正在学编译原理,正则法则,但是汉字判断就不知道了。
java 使用正则表达示判断是不是合法的IPV4IPV6地址
直接看代码:
import java.util.regex.Pattern; public class App { private static Pattern pattern; private static Pattern ptipv4; static { // ipv6 pattern = Pattern.compile("^((([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){1,7}:)|(([0-9A-Fa-f]{1,4}:){6}:[0-9A-Fa-f]{1,4})|(([0-9A-Fa-f]{1,4}:){5}(:[0-9A-Fa-f]{1,4}){1,2})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){1,3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){1,4})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){1,5})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){1,6})|(:(:[0-9A-Fa-f]{1,4}){1,7})|(([0-9A-Fa-f]{1,4}:){6}(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|([0-9A-Fa-f]{1,4}:(:[0-9A-Fa-f]{1,4}){0,4}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}))$"); // ipv4 ptipv4 = Pattern.compile("^(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$"); } public static void main( String[] args ) { System.out.println(pattern.matcher("2001:0DB8:0000:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:8:800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:8:800:192.1.0.0").matches()); System.out.println(pattern.matcher("2001:DB8:0::800:192.1.0.0").matches()); System.out.println(pattern.matcher("2001:DB8:0:23::192.1.0.0").matches()); System.out.println(pattern.matcher("::192.1.0.0").matches()); System.out.println(pattern.matcher("1:af::3").matches()); System.out.println(pattern.matcher("1:af::").matches()); System.out.println(pattern.matcher("::1:af:0").matches()); System.out.println(pattern.matcher("::0").matches()); System.out.println("---------------------"); System.out.println(pattern.matcher("+2001:0DB8:0000:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:0DB8:0z00:0023:0008:0800:200C:417A").matches()); System.out.println(pattern.matcher("2001:DB8:0:23:800::192.1.0.0.1").matches()); System.out.println(pattern.matcher("2001:DB8::23::800:192.1.0.0").matches()); System.out.println(pattern.matcher(":::").matches()); System.out.println(pattern.matcher("1:::2").matches()); System.out.println("---------------------"); System.out.println(ptipv4.matcher("1.1.0.1").matches()); System.out.println(ptipv4.matcher("123.1.0.19").matches()); System.out.println(ptipv4.matcher("255.255.255.255").matches()); System.out.println(ptipv4.matcher("0.0.0.0").matches()); System.out.println("---------------------"); System.out.println(ptipv4.matcher("-1.1.0.1").matches()); System.out.println(ptipv4.matcher("1.1b.0.1").matches()); System.out.println(ptipv4.matcher("1.01.0.1").matches()); System.out.println(ptipv4.matcher("1.1.300.1").matches()); System.out.println(ptipv4.matcher("1.1..1").matches()); } }
以上是关于如何实现用正则表达式判断输个入的姓名是不是合法(中文,2-7个汉字)的主要内容,如果未能解决你的问题,请参考以下文章
java编写正则表达式,判断给定的是不是是一个合法的IP地址