var str = '-abb-aaab-';
console.log('original: ', str);
console.log('------------------------');
console.log(str.replace(/(a)(b)/g, '<$&>')); => -<ab>b-aa<ab>-
console.log(str.replace(/(a)(b)/g,'<$2>')); => -<b>b-aa<b>-
console.log(str.replace(/(a)(b)/g,'<$1>')); => -<a>b-aa<a>-
console.log('------------------------');
console.log(str.replace(/(a)|(b)/g, '<$&>')); => -<a><b><b>-<a><a><a><b>-
console.log(str.replace(/(a)|(b)/g,'<$2>')); => -<><b><b>-<><><><b>-
console.log(str.replace(/(a)|(b)/g,'<$1>')); => -<a><><>-<a><a><a><>-
--------------------------------------------------------------------------------
console.dir(str.match(/(a)|(b)/g)); => [ 'a', 'b', 'b', 'a', 'a', 'a', 'b' ]
沒有留言:
張貼留言