JS正则学习

正则学习

Regex相关

// test
// test就是测试字符串里有没有这个正则,返回布尔值
! function() {
    var reg = /\d+/;
    var flag = reg.test('hello123');
    console.assert(flag);

    flag = reg.test('halo');
    console.assert(!flag);
}();

// exec
// exec就是寻找字符串中符合正则的子字符串
// 返回结果是一个对象,'不是数组'!
! function() {
    // 当正则表达式不带上'g'这个全局标记的时候,它每次都是从头开始找,找到就算
    var reg = /a/;
    var substr = reg.exec('jack is a person!');
    console.assert(substr['0'] == 'a' && substr.index == 1);

    var substr = reg.exec('jack is a person!');
    console.assert(substr['0'] == 'a' && substr.index == 1);


    // 带上'g'全局标记则会一直找下去
    var reg2 = /a/g;
    var substr = reg2.exec('jack is a person!');
    console.assert(substr['0'] == 'a' && substr.index == 1);

    var substr = reg2.exec('jack is a person!');
    console.assert(substr['0'] == 'a' && substr.index == 8);


    // 贪婪(默认贪婪)
    var reg3 = /a+/;
    var substr = reg3.exec('he is aaaa');
    console.assert(substr['0'] == 'aaaa');


    // lastIndex
    // 不是全局模式,lastIndex是没有意义的
    var reg4 = /a/g;
    var str = 'jack is a person';
    // 默认lastIndex是0
    console.assert(reg4.lastIndex == 0);
    reg4.exec(str);
    console.assert(reg4.lastIndex == 2);
    reg4.exec(str);
    console.assert(reg4.lastIndex == 9);

    // 如果不手动修改lastIndex,则换一个字符串进行匹配的时候,还是找不到
    var substr = reg4.exec('a man');
    console.assert(substr === null);
    reg4.lastIndex = 0;
    var substr = reg4.exec('a man');
    console.assert(substr.index === 0 && substr['0'] == 'a');

    // 备注
    // 如果寻找,lastIndex会自动设置为0


}();

// compile
// 重新修改正则匹配内容
! function() {
    var reg = /a/;
    console.assert(reg.test('a'));

    reg.compile('b');
    console.assert(reg.test('b'));
}();

// 其他的配置
// i 忽略大小写
// m 多行分割
! function() {
    // i
    var reg = /abc/i;
    var reg2 = /abc/;
    console.assert(reg.test('Abc'));
    console.assert(!reg2.test('Abc'));

    // m 
    // 不带m,总的匹配即可
    // 带m,被分割的每行中要有一行匹配就可以,或则总的匹配也可以
    var reg = /^a[\S\n]*b$/;
    var reg2 = /^a[\S\n]*b$/m;
    var str = 'akkkkb\ntttttb';
    var str2 = 'akkkk\ntttttb';
    var str3 = 'akkkkb\nttttt';
    console.assert(reg.test(str));
    console.assert(!reg.test(str3));
    console.assert(reg2.test(str2));
    console.assert(reg2.test(str3));

}();

String相关

// search
// 大小写敏感
// 只返回第一个找到的,返回下标索引,找不到返回-1
// (永远从0位置开始找)
! function() {
    var str = 'jack is a person';
    var index = str.search(/a/);
    console.assert(index == 1);
    // 再次查找,还是找到第一个'a'
    var index = str.search(/a/);
    console.assert(index == 1);
}();

// match
// 跟exec很有相似性
! function() {
    var reg = /(\w)\1/;
    var reg2 = /(\w)\1/g;
    var str = 'egg is not an apple, hh';
    var rst = str.match(reg);
    // rst[1]是匹配了其分组
    console.assert(rst.length == 2 && rst[0] == 'gg' && rst[1] == 'g');

    // 全局模式下,不会再去考虑什么分组的匹配
    var rst = str.match(reg2);
    console.assert(rst.length == 3 && rst[0] == 'gg' && rst[1] == 'pp' && rst[2] == 'hh');
}();

// replace
// 去百度面试的题目
! function() {
    var str = 'aaa-bbb';
    // 把'aaa-bbb'转成驼峰写法'aaaBbb'
    var str2 = str.replace(/\-\w/, function(word) {
        return word.replace(/\-/, '').toUpperCase();
    });
    console.assert(str2 === 'aaaBbb');

    // 这个办法更加好,谢谢老邢的指点
    var str3 = str.replace(/-(\w)/, function(a1, a2) {
        return a2.toUpperCase();
    });
    console.assert(str3 === 'aaaBbb');

}();

// split
// split的第二个参数,这里不讨论
! function() {
    var str = 'jack is a person';
    var arr = str.split(/a/);
    console.assert(arr[0] === 'j');
    console.assert(arr[1] === 'ck is ');
    console.assert(arr[2] === ' person');
}();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值