qijiebing / test Goto Github PK
View Code? Open in Web Editor NEW正则笔记
正则笔记
数据类型检测是一个使用比较广的js功能,方法也是老生常谈,目的是为正则抛砖引玉
1 typeof
使用typeof 检测数据类型,首先返回的都是一个字符串,
其次才是字符串包含的对应的数据类型。
例如:‘number,string,boolean’
function fn(callback){
typeof callback =='function'? callback():null;
//callback && callback();
}
局限性:
1 typeof null = object
2 不能具体细分是数组还是正则, 对象数据类型返回的都是object
2 instanceof
var obj = [1,2];
console.log(obj instanceof Array) //true
局限性:
* 对于基础数据类型 字段名方式创建结果和实例创建结果是有一定区别的, 严格意义:只有实例创建的结果才是标准数据类型
* 引用数据类型 instanceof true (array Object function)
* console.log(1 instanceof Number) //false 基础数据类型(‘str ’)这样都false
* console.log(new Number(1) instanceof Number) true
* 只要在原型链上的都是true
3 constructor
* 和 instanceof类似
* var obj = [];
* obj.constructor === Array
* obj.constructor === RegExp
* constructor 可以处理基础数据类型 instanceof 不可用
* 局限性
* 类的原型可以重写 重写可能覆盖constructor 检测结果可能不准确
* function fn(){}
* fn.prototype = new Array
* var f = new fn;
* f.constructor ==Array
* null undefined 不能访问constructor
4 Object.prototype.toString.call 最准确的方式
Object.prototype.toString.call(null)
"[object Null]"
Object.prototype.toString.call(undefined)
"[object Undefined]"
Object.prototype.toString.call([])
"[object array]"
Object.prototype.toString.call(true)
"[object true]"
所有的数据类型都可以检测
var reg = /^\[object Array\]$/;
reg.test(Object.prototype.toString.call(str))
正则是一个规则 用来处理字符串的规则
正则总体分为 匹配、捕获 俩种使用方式
1 匹配
匹配 判断字符串是否符合我们制定的规则 reg.text(str)
var reg = /\d/;
reg.test(1);
2 捕获 把符合我们规则的正则内容捕获到 exec:reg.exec(str)
var reg = /\d/;
console.log(reg.exec('1')) //["1", index: 0, input: "1", groups: undefined]
如何创建一个正则
1 字面量的方式
var reg = /\d/;
2实例创建
var reg = new RegExp("");
3 如何学习正则
// 用面向对象的方法来学习
console.dir(RegExp.prototype)
元字符
每一个正则表达式都是由 元字符 和 修饰符 组成的
[元字符] =》在 / / 中间具有意义的一些字符
1, 具有特殊意义的元字符
** 2 代表出现次数的 量词元字符(6个)**
*: 0-多次出现
+:出现1-多次
?:出现0-1次
{n}:出现n次
{n,}: n到多次
{n,m}:出现n-m次
var reg = /^\d+$/; //一到多个数字
// 验证手机号码
var reg = /^1\d{10}$/
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.