整理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);
}
複製代碼
相關文章
相關標籤/搜索