美文网首页
JS正则的一些体会

JS正则的一些体会

作者: 利312 | 来源:发表于2017-02-07 18:24 被阅读20次

正则说简单也简单,复杂起来也复杂~
看文章,希望大家看英文的原文(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExphttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

  1. 熟悉一些不常用的的
    [\b] \b \B \s \S \n(主要n是数字,代表前面几个) (?:x)
    *, +, ?, and {...} 后加? 为非贪婪(比如 /a+?/),否则贪婪匹配
    x(?=y) x(?!y) 断言正则判断

  2. 属性已经方法
    flags, source, ignoreCase multiline

  3. es6之后
    Symbol.match replace split

  4. test, exec都会改变lastIndex
    一些示例:

  5. 获取url参数

    function getUrlParam(key) {
      var v = location.search.match(new RegExp('[?&]+'+key+'='+'([^&;]+?)(&|#|$)'));
      return v ? decodeURIComponent(v[1]) : '';
    }
  1. 日期format:
// 作者 csdn 的 Meizz
// 对Date的扩展,将 Date 转化为指定格式的String   
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,   
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)   
// 例子:   
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423   
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18   
Date.prototype.Format = function(fmt)   
{ //author: meizz   
  var o = {   
    "M+" : this.getMonth()+1,                 //月份   
    "d+" : this.getDate(),                    //日   
    "h+" : this.getHours(),                   //小时   
    "m+" : this.getMinutes(),                 //分   
    "s+" : this.getSeconds(),                 //秒   
    "q+" : Math.floor((this.getMonth()+3)/3), //季度   
    "S"  : this.getMilliseconds()             //毫秒   
  };   
  if(/(y+)/.test(fmt))   
    fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));   
  for(var k in o)   
    if(new RegExp("("+ k +")").test(fmt))   
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
  return fmt;   
}  
  getUrlParams() {
    var regex = /[?&]([^=#]+)=([^&#]*)/g,
        params = {},
        match;
    while(match = regex.exec(location.search)) {
        params[match[1]] = match[2];
    }
    return params;
  },
  extend() {
    var args = [].slice.call(arguments);
    for(var i=1; i < args.length; i++) {
      for(var k in args[i]) {
        args[0][k] = args[i][k];
      }
    }
    return args[0];
  },

相关文章

  • JS正则的一些体会

    正则说简单也简单,复杂起来也复杂~看文章,希望大家看英文的原文(https://developer.mozilla...

  • 正则表达式

    正则表达式介绍: //正则表达式不是js中的独有功能//其他的一些语言中也有正则的实现方式,当我们在查看关于正则的...

  • 正则初解

    title: js验证常用正则表达式date: 2017-03-03 验证 正则表达式 本文介绍js验证常用的正则...

  • JS正则表达式

    JS正则表达式一条龙讲解,从原理和语法到JS正则、ES6正则扩展,最后再到正则实践思路 Stinson 关注 20...

  • 正则详解--程序员必备

    转自: JS正则表达式一条龙讲解,从原理和语法到JS正则、ES6正则扩展,最后再到正则实践思路 温馨提示:文章很长...

  • 前端学习资源整合(二)

    正则 正则 地址JS正则表达式元字符 http://segmentfault.com/a/119000000247...

  • JS的一些常用正则

    1,得到网页上的链接地址: string matchString = @" ]+href=\s*(?:'(? ']...

  • JS基础

    JS基础 JS运算 JS代码块 JS对象 原型对象 GC 正则表达式

  • js 正则表达式

    js 正则表达式

  • JavaScript基础 JavaScript正则表达式

    JS正则表达式:

网友评论

      本文标题:JS正则的一些体会

      本文链接:https://www.haomeiwen.com/subject/vgiyittx.html