坚果大叔 坚果大叔
  • 首页
  • 闲言碎语
  • 技术
  • 生活
  • 记录
  • 朋友
  • 热点新闻
  • 常用代码
  • 工具箱
  • 关于
首页 › 技术 › JS正则如何限制打折小数点?

JS正则如何限制打折小数点?

坚果大叔
2021-01-03 15:24:44技术阅读 2,826

1、必须包含小数点

let regDis = /^(?=0\.[1-9]|[1-9]\.\d).{3}$|^([2-9])$/

2、非必须包含小数点

let regDis = /^[0-9]{1}(\.[0-9])?$/

3、判断H5是否在小程序webview打开

<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        //ios的ua中无miniProgram,但都有MicroMessenger(表示是微信浏览器)
        wx.miniProgram.getEnv((res)=>{
           if (res.miniprogram) {
               alert("在小程序里");
           } else {
               alert("不在小程序里");
           }
        })
    }else{
        alert('不在微信里');
    }
</script>

4、window.open()跳转被拦截!

通过异步请求获取跳转链接后再进行window.open跳转会被浏览器拦截。

解决方案

export default {
  data() {
    return {
      jumpUrl: '',
      newWin: null  // 新窗口的引用
    }
  },
  watch: {
    url(newVal, oldVal) {
      if(newVal && this.newWin) {
        this.newWin.sessionStorage.clear()
        this.newWin.location.href = newVal;
        this.jumpUrl = '';
        this.newWin = null;
      }
    }
  },
  methods: {
    clickHandle() {
      let _this = this;
      // 先打开一个空的新窗口,再请求
      this.newWin = window.open();
      api.get('xxx', params).then(response => {
        let data = response.data;
        if(data.code === 0 ) {
          _this.jumpUrl = data.data.url || '';
        }
      });
    }
  }
}

6、数组对象去重

export function deteleObject(obj) {
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {
        var keys = Object.keys(obj[i]);
        keys.sort(function(a, b) {
            return (Number(a) - Number(b));
        });
        var str = '';
        for (var j = 0; j < keys.length; j++) {
            str += JSON.stringify(keys[j]);
            str += JSON.stringify(obj[i][keys[j]]);
        }
        if (!stringify.hasOwnProperty(str)) {
            uniques.push(obj[i]);
            stringify[str] = true;
        }
    }
    uniques = uniques;
    return uniques;
}
  deteleObject(obj) {
    var hash = {};
    obj = obj.reduce(function(item, next) {
        hash[next.openid] ? '' : hash[next.openid] = true && item.push(next);
        return item
    }, [])
    return obj
  },
正则
赞赏 赞(0)
本文系作者 @坚果大叔 原创发布在 坚果大叔。未经许可,禁止转载。
一年一年又一年
上一篇
高处,不一样的风景
下一篇
在小程序中查看

评论已关闭

搜你想看的
聚合文章
镜头下的一点记录!
JavaScript之继承(未完待续)
如何获取微信文章封面和文章标题!
火狐 window.event无效问题
闲言碎语
Memos
npm install 出现 Error:EISDIR:illegal operation on a directory 的错误提示!
2025-03-31 19:02:11
786 0 0
iphone 利用 Scriptable 添加网上国网电费小组件
2025-01-06 20:40:20
6,501 2
在博客中加上memos记录展示。
2024-12-25 23:41:27
3,201 2
网页点击平滑滚动效果~
2024-10-17 16:43:08
2,335 4
  • 0
博主

一位佛系的前端开发者,略通摄影,乐于尝试新事物,热衷于美食。

友链
故事胶片
公众号
坚果大叔 执行上下文 卖坚果的怪叔叔 Dacking
Copyright © 2017-2025 坚果大叔

开往-友链接力

萌ICP备20230818号

苏ICP备18048410号-3
  • 首页
  • 闲言碎语
  • 技术
  • 生活
  • 记录
  • 朋友
  • 热点新闻
  • 常用代码
  • 工具箱
  • 关于
# CSS # # JavaScript # # vue # # 微信 # # 生活 #
坚果大叔
318
文章
131
评论
370
喜欢