首页 > 编程语言 >微信小程序中data里的正则表达式丢失问题

微信小程序中data里的正则表达式丢失问题

时间:2022-12-28 21:11:22浏览次数:62  
标签:string 正则表达式 微信 null data reg

最近在开发微信小程序的时候在data里面定义了正则表达式,结果在读取的时候发现正则表达式丢了。只返回了一个空的对象

Page({
  data: {
    reg: /^1\d{10}$/g
  },
  onl oad() {
     console.log(this.data.reg) //打印结果: {}
  }
})

正则不支持的解决方法:
存储的时候存成字符串:reg: '^1\d{10}$',使用的时候进行new RegExp(reg)就可以进行使用了。
参考微信小程序开放平台提问
微信小程序官方文档
页面加载时,data 将会以JSON字符串的形式由逻辑层传至渲染层,因此data中的数据必须是可以转成JSON的类型:字符串,数字,布尔值,对象,数组。

// index.js
Page({
  data: {
    number: 1,
    string: 'string',
    boolean: true,
    undef: undefined,
    null: null,
    object: {
      a: '1'
    },
    array: [1, 2, 3],
    fun: function () {
      console.log(1)
    },
    reg: new RegExp('/^1/d{10}/g'),
    date: new Date(),
    symbol: Symbol('name'),
    // bigint: BigInt(Number.MAX_SAFE_INTEGER)
  },

  onl oad() {
    console.log(this.data)
  }
})

打印结果:

{
  array: (3) [1, 2, 3]
  boolean: true
  date: {}
  fun: ƒ fun()
  null: null
  number: 1
  object: {a: "1"}
  reg: {}
  string: "string"
  symbol: undefined
  undef: undefined
  bigint: // 报错
}

由上可知在data中date、reg、symbol数据类型都不支持,小程序中不支持bigint数据类型。

标签:string,正则表达式,微信,null,data,reg
From: https://www.cnblogs.com/mynl/p/17011287.html

相关文章