news 2026/6/1 18:31:16

HarmonyOS RegexUtil 身份证验证与内置正则常量:isValidCard 和 isMatch 实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
HarmonyOS RegexUtil 身份证验证与内置正则常量:isValidCard 和 isMatch 实战

文章目录

    • 背景
      • 方法总览
      • 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)true
  • isMatch("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)true
  • isMatch("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_IPV4IPv4 地址192.168.1.1
REG_IPV6IPv6 地址::1
REG_URL各类 URLhttps://example.com
REG_URL_HTTPHTTP/HTTPS URLhttps://example.com
REG_CHINESES纯中文字符你好
REG_WORD纯字母hello
REG_UUIDUUID 格式550e8400-...
REG_ZIP_CODE6位邮政编码100000
REG_BIRTHDAY生日(年/月/日)1990/01/01
REG_PLATE_NUMBER中国车牌号粤A12345
REG_CREDIT_CODE统一社会信用代码91310000...
REG_TIME时间格式 HH:mm:ss12:30:00

写在最后

isMatch配合内置常量,几乎覆盖了国内应用开发里所有常见的格式验证场景。遇到这些验证需求,直接用现成的,不要再自己写正则了——手写正则一不小心就会有漏洞。

下一篇讲isMatch自定义正则——当内置常量不够用时,怎么传自己的正则来验证。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/1 18:30:26

抖音批量下载终极指南:如何5分钟搞定创作者素材库

抖音批量下载终极指南:如何5分钟搞定创作者素材库 【免费下载链接】douyin-downloader A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback support. …

作者头像 李华
网站建设 2026/5/29 10:12:10

零代码AI翻唱神器:5分钟让任何声音唱出你的最爱歌曲

零代码AI翻唱神器:5分钟让任何声音唱出你的最爱歌曲 【免费下载链接】AICoverGen A WebUI to create song covers with any RVC v2 trained AI voice from YouTube videos or audio files. 项目地址: https://gitcode.com/gh_mirrors/ai/AICoverGen 你是否曾…

作者头像 李华