数组的迭代方法与正则

目录

一、数组的迭代方法

1、forEach

2、map

3、filter

4、every

5、some

6、find和findIndex

二、正则

1、写法

2、修饰符

3、正则的方法

test

exec

match

split

replace

search

4、正则相关实例


一、数组的迭代方法

1、forEach

// 数组.forEach(function (item, index, array) { });
// 接收一个函数做为参数,该函数有三个参数,分别为数组项,下标,数组本身
// 作用:循环数组(没有返回值),代替for循环

var arr = ['a', 'b', 'c'];
arr.forEach(function (item, index, array) {
    console.log(item, index, array);
});

2、map

// 数组.map(function (item, index, array) { });
// 接收一个函数做为参数,该函数有三个参数,分别为数组项,下标,数组本身
// 作用:循环数组,返回每次函数调用的结果组成的一个新数组
var arr = [4, 2, 6];
var a = arr.map(function (item, index, array) {
    return item * 2;
});
console.log(a); // [8, 4, 12]

3、filter

// 数组.filter(function (item, index, array) { });
// 接收一个函数做为参数,该函数有三个参数,分别为数组项,下标,数组本身
// 作用:循环数组,返回每次函数调用的结果为true的项组成的一个新数组
var arr = [4, 8, 2, 6];
var a = arr.filter(function (item) {
    return item > 5;
});
console.log(a);

4、every

// 数组.every(function (item, index, array) { });
// 接收一个函数做为参数,该函数有三个参数,分别为数组项,下标,数组本身
// 作用:循环数组,如果每次函数调用结果都为true,则返回true
var arr = [4, 8, 2, 6];
var a = arr.every(function (item, index, array) {
    return item > 5;
});
console.log(a);

5、some

// 数组.some(function (item, index, array) { });
// 接收一个函数做为参数,该函数有三个参数,分别为数组项,下标,数组本身
// 作用:循环数组,函数调用只要有一个为true,则结果为true
var arr = [4, 8, 2, 6];
var a = arr.some(function (item, index, array) {
    return item > 10;
});
console.log(a);

6、find和findIndex

// 数组.find(function (item, index, array){});    
// 循环数组,调用函数,如果这个函数返回true,则把对应的item返回,否则返回undefined
// 返回的是满足条件的这一项

var arr = [3, 4, 3, 2, 3, 2, 3, 3];
var a = arr.find(function (item, index, array) {
    // console.log(item, index, array);
    return item === '3';
});
console.log(a);

// ---------------------------
var arr = [
    { name: 'zs', password: 1234 },
    { name: 'ls', password: 4545 },
    { name: 'ww', password: 77 },
    { name: '小王', password: 3333 },
    { name: '小李', password: 8998 }
];
var o = arr.find(function (item, index, array) {
    return item.name == 'ww' && item.password == 77
})
console.log(o);



// 数组.findIndex(function (item, index, array){}); 
// 调用函数,如果这个函数返回true,则把对应的item的下标返回,否则返回-1
// 返回的是满足条件的下标
var o = arr.findIndex(function (item, index, array) {
    return item.name == '小王' && item.password == 3333;
});
console.log(o);

二、正则

1、写法

  • 字面量创建: /检索字符/修饰符;

  • 构造函数创建:new RegExp('检索字符', '修饰符');

// 1、字面量
// /检索字符/修饰符
var re = /abc/;
console.log(re); // /abc/
console.log(typeof re); // 'object'

// --------------------------------------

// 2、构造函数(如果有变量,就必须用构造函数创建,不能用字面量创建)
// new RegExp('检索字符', '修饰符')
var re = new RegExp('abc');
console.log(re); // /abc/
console.log(typeof re); // 'object'


// -------------
// 使用变量
var ddd = 'abc';
var re = new RegExp(ddd);
console.log(re);

2、修饰符

var str = '今天上课,明天上课,后天上课';

// 正则默认只匹配成功一次,就会停止,如果想多次匹配,则加上标识符g
var re1 = /上课/;
var re2 = /上课/g; // 全局匹配
var re3 = new RegExp('上课', 'g');

console.log(str.replace(re1, '休息')); // 今天休息,明天上课,后天上课
console.log(str.replace(re2, '休息')); // 今天休息,明天休息,后天休息
console.log(str.replace(re3, '休息')); // 今天休息,明天休息,后天休息


// ---------------------------------------------
// 正则默认区分大小写,如果想要不区分大小写,则加标识符i
var str = 'abCd';
var re1 = /abcd/;
var re2 = /abcd/ig;
var re3 = new RegExp('abcd', 'ig');

console.log(re1.test(str)); // false
console.log(re2.test(str)); // true

3、正则的方法

test

// 正则.test(字符串)     常用
// 检索字符串中是否包含正则要检索的内容,有则返回 true,没有则返回 false


var str = "hello world";
var re = /o/;
console.log(re.test(str)); // true

exec

// 正则.exec(字符串)      了解
// 检索到正则表达式规定的内容会返回一个数组,检索不到则返回 null
// 同match

var str = "hello world";

var re = /o/;
var re1 = /z/;

console.log(re.exec(str)); // [ "o" ]
console.log(re1.exec(str)); // null

match

// 字符串.match(正则)
// 正则去匹配字符串,如果匹配成功,就返回匹配成功的数组,如果匹配不成功,就返回null,跟exec方法一样

var str = "hello world";
var re = /o/g;
console.log(str.match(re)); // [ "o", "o" ]

// ------------------------------------------
var str = 'haj123sdk54hask33dkhalsd879';
var re = /\d+/g;
console.log(str.match(re)); // ["123", "54", "33", "879"]

split

// 字符串.split(正则);
// 按正则匹配的内容拆分成数组

var str = 'hello web';
console.log(str.split(' '));
console.log(str.split(/\s/));

// ------------------------------
// |   :正则中的或者

var str = '2023-12-12 10:10:10'; // [2023,12,12,10,10,10]
var re = /\s|-|:/;
console.log(str.split(re)); // ["2023", "12", "12", "10", "10", "10"]

replace

// 字符串.split(正则);
// 按正则匹配的内容拆分成数组

var str = 'hello web';
console.log(str.split(' '));
console.log(str.split(/\s/));

// ------------------------------
// |   :正则中的或者

var str = '2023-12-12 10:10:10'; // [2023,12,12,10,10,10]
var re = /\s|-|:/;
console.log(str.split(re)); // ["2023", "12", "12", "10", "10", "10"]
// 字符串.search(正则);
// 返回正则匹配到的第一个字符串的位置,没有返回-1,类似于indexOf

var str = 'hello world';

var re1 = /o/;
var re2 = /z/;
console.log(str.search(re1)); // 4
console.log(str.search(re2)); // -1

4、正则相关实例

// 邮编
var re = /^\d{6}$/;


// 匹配中文
var str = 'fdssd小sfsdfddsfdsafsdsf芳ds约fdsfdsa不fds';
var re = /[\u4e00-\u9fa5]/g; // 有事一00,有酒罚我
console.log(str.match(re).join(''));

// 电子邮箱
var str = '23432432@qq.com.cn';
var str = 'sdf@163.com';
var re = /^\w+@[a-z0-9]+(\.[a-z]+){1,3}$/;


// 网址
// https://www.baidu.com
var re = /^[a-zA-Z]+:\/\/[^\s]+$/;

// 身份证号
var re = /[1-9]\d{14}|[1-9]\d{17}|[1-9]\d{16}x/;

// 电话号码
var re = /^0\d{2,3}-[1-9]\d{6,7}$/;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值