Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Created January 2, 2014 04:53
Show Gist options
  • Save alanerzhao/8215226 to your computer and use it in GitHub Desktop.
Save alanerzhao/8215226 to your computer and use it in GitHub Desktop.
判断
/*
test 检查指定的字符串是否存在
exec 返回查询值
match 得到查询数组
search 返回搜索位置
replace 替换字符 利用正则替换
split 利用正则分割数组
*/
var str = "Str",
num = 1123,
numstr = "123123",
arr = [],
obj = {},
fun = function () {},
reg = /[a-z]/,
bb;
//因为Object.prototype.toString()方法被设计用来返回对象类型的。String、Array、Boolean、Regexp、Number和Function都继承自Object,同时也就继承了Object的原型方法toString(),但是他们都对toString()进行了重写。执行xxx.toString()时使用的是重写后的方法,返回的结果自然会和Object.prototype.toString.call()的结果不一致
function isArray(obj){
if(obj instanceof Array && Object.prototype.toString.call(obj) == '[object Array]'){
return 'array';
}
}
function isNum(obj){
if(typeof obj == "number") {
return "number"
}
}
function isObj(obj){
if(Object.prototype.toString.call(obj) == '[object Object]'){
return 'object';
}
}
function isFun(obj){
if(Object.prototype.toString.call(obj) == '[object Function]'){
return 'function';
}
}
function isReg(obj){
if(Object.prototype.toString.call(obj) == '[object RegExp]'){
return 'RegExp';
}
}
function isUn(obj){
if(typeof obj == "undefined"){
return 'undefined';
}
}
console.log(isArray(arr));
console.log(isNum(num));
console.log(isObj(obj));
console.log(isFun(fun));
console.log(isReg(reg));
console.log(isUn(bb));
var reg = /ab+/gim;
var reg = /\\/
var str = 'aaabbb';
var cc = '\\';
console.log(reg.test(cc))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment