整理js 框架里面经常使用正则

1.必须包含大小写数字 8-16位html

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z0-9]{8,16}$/复制代码

2.数字千分位展现bash

function numberWithCommas(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}复制代码

3.转义须要转义的ui

function escapeRegexp(queryToEscape) {
  return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1')
}复制代码

4. 连字符转驼峰 ad-bd-cd to adBdCdspa

var camelizeRE = /-(\w)/g;
var camelize = function (str) {
  return str.replace(camelizeRE, function (_, c) {
    return c ? c.toUpperCase() : '';
  })
}复制代码

5.驼峰转连字符code

var hyphenateRE = /\B([A-Z])/g;
var hyphenate = function (str) {
  return str.replace(hyphenateRE, '-$1').toLowerCase()
};复制代码


6.转义htmlhtm

function htmlEscape(text){
  return text.replace(/[<>"&]/g, function(match, pos, originalText){ console.log(match, pos, originalText) switch(match){ case "<": return "&lt;"; case ">":return "&gt;"; case "&":return "&amp;"; case "\"":return "&quot;";
    }
  });
}复制代码

7.判断相对 绝对路径string

var absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|/])/;
var relativePath = /^\.?\.\//;
function isAbsolute(path$$1) {
  return absolutePath.test(path$$1);
}
function isRelative(path$$1) {
  return relativePath.test(path$$1);
}
复制代码
相关文章
相关标签/搜索