正则表达式:

什么是RegExp?

RegExp 是正则表达式的缩写。

定义RegExp

通过new关键词来定义。

写法:

let reg:RegExp=new RegExp('a');

RegExp对象有三个方法.

1.test()、exec() 以及 compile()。

test和exec

字符是否存在

返回值是true或false.

写法:

console.log(reg.test('acdasdae'));
console.log(reg.exec('acdasdae'));

改变正则规则

写法:

reg3.compile('as')
console.log(reg3.exec('Asdasdasd'));

修饰符:

1.i 忽略大小写

写法

let reg2:RegExp=new RegExp('a','i');

2.g 全局匹配

写法:

let reg3:RegExp=new RegExp('a','g');
console.log(reg2.exec('Asd'));
console.log(reg3.exec('Asdasdasd'));

方括号:

1.[] 代表一个字符

写法:

console.log('as'.match('^[asdf]$'));
console.log('12'.match('^[0-9][0-9]$'));
console.log('a'.match('^[a-z]$'));
console.log('A'.match('^[A-Z]$'));

单词字符 大小写 _ (下划线)数字(不包含)

写法:

console.log('_'.match('^[A-z]$'));
console.log('张'.match('^[张王李赵]$'));

例子

规定取款密码 6个数字

写法:

let pwd:string='754654';
console.log(pwd.match('^[0-9][0-9][0-9][0-9][0-9][0-9]$'));

判断 姓名:是不是张xx

写法:

console.log('张三丰'.match('^张..$'));

判断手机号

写法:

console.log('19999999999'.match('^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'));

量词:

1.+至少一个

写法:

console.log('123'.match('^[0-9]+$'));

2. *零或多个

写法:

console.log('a'.match('^a[0-9]*$'));

3.零个或一个 n

写法:

console.log(''.match('^[A-z]?$'));

4.正好 n 个

写法:

console.log('123456'.match('^[0-9]{6}$'));
console.log('19999999999'.match('^1[0-9]{10}$'));

3.x到y个

写法:

console.log('aaaaaa1'.match('^[A-z0-9]{6,12}$'));

4.最少 n 个

写法:

console.log('a'.match('^[A-z0-9]{6,}$'));

元字符:

1.\\w单词字符  \\W非单词

写法

console.log('aaaasdf'.match('^\\w{6,12}$'));

2.\d数字  \D非数字

写法:

console.log('asddfd'.match('^\\D{6}$'));

3.邮箱 \\.代表'.'本身 转译

写法:

console.log('asda12323@qwad.com'.match('^\\w+@\\w+\\.\\w+$'));

例子:

1.身份证

写法:

console.log('123456789012345678'.match('^\\d{17}[0-9Xx]$'));

2.手机号:

写法:

console.log('181245354783'.match('^1[3-9]\\d{9}$'));

3.出生日期  年  月   日   1900~2024  ()表示一个整体

写法:

console.log('1809'.match('^19\\d{2}$'));
console.log('2021'.match('^20(([01][0-9])|(2[0-4]))$'));
console.log('1809'.match('^(19\\d{2})|(20(([01][0-9])|(2[0-4])))$'));

4.月 01~12

写法:

console.log('12'.match('^(0[1-9])|(1[0-2])$'));

5.日 01~31

写法:

console.log('1'.match('^(0[1-9])|([12][0-9])|(3[01])$'));

6.001~999 [0-9][0-9][0-9]  001~009 010~999   010~099 100~999

写法:

console.log('121'.match('^(00[1-9])|(0[1~9]\\d)|([1-9]\\d{2})$'));

7.姓名: 查找姓张的人    

写法:

console.log('张ZZ'.match('^张.+$'));

8.查找名字中带'铮'的人

写法:

console.log('铮'.match('^.*铮.*$'));

用正则表达式写代码:

写法:

@Entry
@Component
struct Reg2Page {
  @State message: string = '账号注册';
  @State val: string[]=['','','','','','',''];//默认值
  @State isFlag: number[]=[0,0,0,0,0,0,0]//0默认第一次进入 1验证成功
  @State Msg: string []= ['','','','','','','']//提示
  @State msgErr:string[]=['账号为6~12为单词字符','密码长度6~12','密码不一致','手机号不正确','身份证不正确','昵称违规','邮箱必须包含@.'];//错误提示
  @State pat:string[]=['^\\w{6,12}$','^\\w{6,12}$','^\\w{6,12}','^\\d{10}$','^\\d{17}[0-9Xx]$','^.+$','^\\w+@\\w+\\.\\w+$',]
  @State asdd:string[]=['账号不能为空','密码不能为空','确认密码不能为空','手机号不能为空','身份证不能为空','昵称不能为空','邮箱不能为空']
  build() {
    Column() {
      Text(`${this.message}`).fontSize(30)

      //账号
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '账号' }).width('90%').borderRadius(0)
            .onChange(Val => this.val[0] = Val)
          if (this.isFlag[0] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[0]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }

        }.width('100%')

        Text(`${this.Msg[0]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //密码
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '密码' }).width('90%').borderRadius(0).type(InputType.Password)
            .onChange(Val => this.val[1] = Val)
          if (this.isFlag[1] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[1]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }

        }.width('100%')

        Text(`${this.Msg[1]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //确认密码
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '确认密码' }).width('90%').borderRadius(0).type(InputType.Password)
            .onChange(Val => this.val[2] = Val)
          if (this.isFlag[2] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[2]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }


        }.width('100%')

        Text(`${this.Msg[2]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //手机号
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '手机号' }).width('90%').borderRadius(0)
            .onChange(Val => this.val[3] = Val)
          if (this.isFlag[3] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[3]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }

        }.width('100%')

        Text(`${this.Msg[3]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //身份证
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '身份证' }).width('90%').borderRadius(0)
            .onChange(Val => this.val[4] = Val)
          if (this.isFlag[4] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[4]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }


        }.width('100%')

        Text(`${this.Msg[4]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //昵称
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '昵称' }).width('90%').borderRadius(0)
            .onChange(Val => this.val[5] = Val)
          if (this.isFlag[5] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[5]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }

        }.width('100%')

        Text(`${this.Msg[5]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      //邮箱
      Column({ space: 10 }) {
        Row() {
          TextInput({ placeholder: '邮箱' }).width('90%').borderRadius(0)
            .onChange(Val => this.val[6] = Val)
          if (this.isFlag[6] == 1) {
            Text('√')
              .fontColor('#fff')
              .fontSize(20)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('blue')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          } else if (this.isFlag[6]==2) {
            Text('×')
              .fontColor('#fff')
              .fontSize(25)
              .fontWeight(900)
              .width(30)
              .height(30)
              .backgroundColor('red')
              .borderRadius(30)
              .textAlign(TextAlign.Center)
          }

        }.width('100%')

        Text(`${this.Msg[6]}`).fontColor('red').width('100%')
      }.width('90%').height(70)

      Button("注册").type(ButtonType.Normal).width('80%')
        .onClick(() => {
          for(let i=0;i<this.val.length;i++){
            if(this.val[i].length>0) {
              if(i==2){//再次输入密码
                if(this.val[1]==this.val[2]){
                  this.isFlag[2]=1;
                  this.Msg[2]=''
                }else {
                  this.isFlag[2]=2;
                  this.Msg[2]='两次密码不一致'
                }
                continue
              }
              let flag:boolean=this.patt(i);
            }else{
              this.isFlag[i]=2;
              this.Msg[i]=this.asdd[i]
            }

          }

        })

      //是否同意本协议

    }
    .height('100%')
    .width('100%')
  }
  //验证  (下标,规则,组件,)
  patt(index:number):boolean{
    if(this.val[index].match(this.pat[index])){ //符合规则
      this.isFlag[index]=1;
      this.Msg[index]=''
      return true
    }else {
      this.isFlag[index]=2;
      this.Msg[index]=this.msgErr[index]
      return false
    }
  }



}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值