正则表达式exec方法

by noahua

JavaScript

/**************************************************************

                      强大的正则exec方法

*************************************************************/

var str = "012345678901234567890";
exec_global();
exec_once();
match_global();
match_once();

function exec_global() {
    var reg = /56/g;

    console.log(arguments.callee)
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第一次] ', reg.exec(str))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第二次] ',reg.exec(str))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('----------------------------------------------------------')
}

function exec_once() {
    var reg = /56/;

    console.log(arguments.callee)
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第一次] ', reg.exec(str))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第二次] ',reg.exec(str))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('----------------------------------------------------------')
}

function match_global() {
    var reg = /56/g;

    console.log(arguments.callee)
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第一次] ', str.match(reg))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第二次] ',str.match(reg))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('----------------------------------------------------------')
}

function match_once() {
    var reg = /56/;

    console.log(arguments.callee)
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第一次] ', str.match(reg))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('[正则匹配第二次] ',str.match(reg))
    console.log('[lastIndex] ' + reg.lastIndex);
    console.log('----------------------------------------------------------')
}