文章目录
- 背景
- 方法总览
- isValidCard:身份证验证
- isMatch:通用正则匹配
- 内置正则常量大全
- 内置正则常量速查表
- 写在最后
背景
近期发现一款很有意思的HarmonyOS 三方库, 地址 @pura/harmony-utils(V1.4.0) , 作者是"桃花镇童长老", 我这里也是直接通过该作者公布的源码进行案例编写进行,写了到目前写了一部分demo ,感觉确实很有帮助,这里呢也是开始写一个系列的演示demo 供大家参考。如有帮助可以在OpenHarmony中进行下载安装进行使用哦
案例demo导航展示
↓↓↓↓↓↓接下来言归正传 ↓↓↓↓
上一篇讲了手机号和邮箱验证。这篇继续讲RegexUtil里另外两个重要功能:身份证验证isValidCard,以及配合内置正则常量使用的isMatch。
内置正则常量这部分内容很丰富,IP、URL、中文、UUID、邮编、车牌号、身份证……一网打尽。
方法总览
isValidCard:身份证验证
中国居民身份证号码格式:
- 18位:6位地区码 + 8位出生日期 + 3位顺序码 + 1位校验码
- 15位:老版身份证(现在基本不用了)
this.Btn('isValidCard() 验证输入','#E74C3C',()=>{this.addLog(`isValidCard("${this.inputCard}") →${RegexUtil.isValidCard(this.inputCard)}`);})this.Btn('isValidCard("110101199001011234") 15/18位测试','#C0392B',()=>{this.addLog(`isValidCard("110101199001011234") →${RegexUtil.isValidCard('110101199001011234')}`);})实际运行结果:
isValidCard("110101199001011234")→true(格式正确的18位)isValidCard("12345")→false(位数不对)isValidCard("110101199001019999")→false(校验码不对)
注意:isValidCard不只是检查位数,还会验证校验码。所以随便凑18位数字也会返回false。
isMatch:通用正则匹配
isMatch(str, pattern)是一个通用匹配方法,pattern可以是字符串或内置正则常量。
基本用法:
// 用字符串正则RegexUtil.isMatch("hello","^[a-z]+$")// → true// 用内置正则常量RegexUtil.isMatch("192.168.1.1",RegexUtil.REG_IPV4)// → true内置正则常量大全
RegexUtil内置了大量常用正则,不需要自己写:
IP 地址验证:
// IPv4this.Btn('isMatch(ip, REG_IPV4)','#2471A3',()=>{this.addLog(`isMatch("${this.inputIP}", REG_IPV4) →${RegexUtil.isMatch(this.inputIP,RegexUtil.REG_IPV4)}`);})// IPv6this.Btn('isMatch("::1", REG_IPV6)','#2471A3',()=>{this.addLog(`isMatch("::1", REG_IPV6) →${RegexUtil.isMatch('::1',RegexUtil.REG_IPV6)}`);})isMatch("192.168.1.1", REG_IPV4)→trueisMatch("256.0.0.1", REG_IPV4)→false(256超出范围)isMatch("::1", REG_IPV6)→true(本地IPv6地址)
URL 验证:
this.Btn('isMatch("https://example.com", REG_URL_HTTP)','#1A6BB5',()=>{this.addLog(`isMatch("https://example.com", REG_URL_HTTP) →${RegexUtil.isMatch('https://example.com',RegexUtil.REG_URL_HTTP)}`);})this.Btn('isMatch("not-a-url", REG_URL) ✗','#1A6BB5',()=>{this.addLog(`isMatch("not-a-url", REG_URL) →${RegexUtil.isMatch('not-a-url',RegexUtil.REG_URL)}`);})isMatch("https://example.com", REG_URL_HTTP)→trueisMatch("ftp://files.example.com", REG_URL)→true(REG_URL 支持更多协议)isMatch("not-a-url", REG_URL)→false
中文字符检测:
this.Btn('isMatch("你好", REG_CHINESES) ✓','#148F77',()=>{this.addLog(`isMatch("你好", REG_CHINESES) →${RegexUtil.isMatch('你好',RegexUtil.REG_CHINESES)}`);})this.Btn('isMatch("hello", REG_WORD) 纯字母 ✓','#148F77',()=>{this.addLog(`isMatch("hello", REG_WORD) →${RegexUtil.isMatch('hello',RegexUtil.REG_WORD)}`);})isMatch("你好世界", REG_CHINESES)→true(全是中文)isMatch("hello", REG_WORD)→true(纯字母)
UUID 验证:
this.Btn('isMatch(uuid, REG_UUID)','#196F3D',()=>{constuuid='550e8400-e29b-41d4-a716-446655440000';this.addLog(`isMatch("${uuid}", REG_UUID) →${RegexUtil.isMatch(uuid,RegexUtil.REG_UUID)}`);})isMatch("550e8400-e29b-41d4-a716-446655440000", REG_UUID)→true
邮政编码:
this.Btn('isMatch("100000", REG_ZIP_CODE)','#7E5109',()=>{this.addLog(`isMatch("100000", REG_ZIP_CODE) →${RegexUtil.isMatch('100000',RegexUtil.REG_ZIP_CODE)}`);})isMatch("100000", REG_ZIP_CODE)→true(北京邮编)
生日格式:
this.Btn('isMatch("1990/01/01", REG_BIRTHDAY)','#7E5109',()=>{this.addLog(`isMatch("1990/01/01", REG_BIRTHDAY) →${RegexUtil.isMatch('1990/01/01',RegexUtil.REG_BIRTHDAY)}`);})isMatch("1990/01/01", REG_BIRTHDAY)→true
车牌号:
this.Btn('isMatch("粤A12345", REG_PLATE_NUMBER)','#7D6608',()=>{this.addLog(`isMatch("粤A12345", REG_PLATE_NUMBER) →${RegexUtil.isMatch('粤A12345',RegexUtil.REG_PLATE_NUMBER)}`);})isMatch("粤A12345", REG_PLATE_NUMBER)→true
企业统一社会信用代码:
this.Btn('isMatch(信用代码, REG_CREDIT_CODE)','#7D6608',()=>{constcode='91310000717774966B';this.addLog(`isMatch("${code}", REG_CREDIT_CODE) →${RegexUtil.isMatch(code,RegexUtil.REG_CREDIT_CODE)}`);})isMatch("91310000717774966B", REG_CREDIT_CODE)→true
时间格式:
this.Btn('isMatch("12:30:00", REG_TIME) ✓','#1C2833',()=>{this.addLog(`isMatch("12:30:00", REG_TIME) →${RegexUtil.isMatch('12:30:00',RegexUtil.REG_TIME)}`);})isMatch("12:30:00", REG_TIME)→true
内置正则常量速查表
| 常量 | 验证内容 | 示例 |
|---|---|---|
REG_IPV4 | IPv4 地址 | 192.168.1.1 |
REG_IPV6 | IPv6 地址 | ::1 |
REG_URL | 各类 URL | https://example.com |
REG_URL_HTTP | HTTP/HTTPS URL | https://example.com |
REG_CHINESES | 纯中文字符 | 你好 |
REG_WORD | 纯字母 | hello |
REG_UUID | UUID 格式 | 550e8400-... |
REG_ZIP_CODE | 6位邮政编码 | 100000 |
REG_BIRTHDAY | 生日(年/月/日) | 1990/01/01 |
REG_PLATE_NUMBER | 中国车牌号 | 粤A12345 |
REG_CREDIT_CODE | 统一社会信用代码 | 91310000... |
REG_TIME | 时间格式 HH:mm:ss | 12:30:00 |
写在最后
isMatch配合内置常量,几乎覆盖了国内应用开发里所有常见的格式验证场景。遇到这些验证需求,直接用现成的,不要再自己写正则了——手写正则一不小心就会有漏洞。
下一篇讲isMatch自定义正则——当内置常量不够用时,怎么传自己的正则来验证。