Javascript 正则运用第一篇

replace()

参数申明

  • @param match
    The matched substring. (Corresponds to $&.)

  • @param p1

  • @param p2
  • @param p3
    The nth parenthesized submatch string,
    provided the first argument to replace was a RegExp object.
    (Corresponds to $1, $2, etc. above.) For example,
    if /(\a+)(\b+)/, was given, p1 is the match for \a+, and p2 for \b+.

  • @param offset
    The offset of the matched substring within the total string being examined. (For example,
    if the total string was “abcd”,
    and the matched substring was “bc”,
    then this argument will be 1.)

  • @param string
    The total string being examined.

  • @returns {*|string}

function replacer(match, p1, p2, p3, offset, string) {     
    console.log('match: ' + match);
    console.log('string: ' + string);
    //p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log('p1: ' + p1);
    console.log('p2: ' + p2);
    console.log('p3: ' + p3);   
    //此处赋值,为了更好的视察p1,p3的值,位置
    p1 = 1;
    p3 = 3;    
   return [p1, p2, p3].join(' - ');
}
newString = "111abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);

运转效果:

 * match: 111
 * string: 111abc12345#$*%
 * p1:
 * p2: 111
 * p3:
 * 1 - 111 - 3abc12345#$*%
function replacer2(match, p1, p2, p3, offset, string) {
    console.log('match: ' + match);
    console.log('string: ' + string);
    // p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log('p1: ' + p1);
    console.log('p2: ' + p2);
    console.log('p3: ' + p3);
    return [p1, p2, p3].join(' - ');
}
newString2 = "abc12345xxx#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer2);
console.log(newString2);

运转效果:

 * match: abc12345
 * string: abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:
 * abc - 12345 - xxx#$*%
function replacer3(match, p1, p2, p3, offset, string) {
    console.log('match: ' + match);
    console.log('string: ' + string);
    // p1 is nondigits, p2 digits, and p3 non-alphanumerics
    console.log('p1: ' + p1);
    console.log('p2: ' + p2);
    console.log('p3: ' + p3);
    return [p1, p2, p3].join(' - ');
}
newString3 = "1111abc12345xxx#$*%2392039abc12345xxx#$*%".replace(/([^\d]*)(\d*)([^\w]*)/g, replacer3);
console.log(newString3);

运转效果:

 * 第一次婚配

 * match: 1111 
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1:
 * p2: 1111
 * p3:

 * 第二次婚配

 * match: abc12345
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:


 * 第三次婚配

 * match: xxx#$*%2392039
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: xxx#$*%
 * p2: 2392039
 * p3:


 * 第四次婚配

 * match: abc12345
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: abc
 * p2: 12345
 * p3:

 * 第五次婚配

 * match: xxx#$*%
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1: xxx#$*%
 * p2:
 * p3:

 * 第六次婚配

 * match:
 * string: 1111abc12345xxx#$*%2392039abc12345xxx#$*%
 * p1:
 * p2:
 * p3:
 * - 1111 - abc - 12345 - xxx#$*% - 2392039 - abc - 12345 -     xxx#$*% -  -  -  -

match()

function match1() {
    var str = "For more information, see Chapter 3.4.5.1",
        re = /(chapter \d+(\.\d)*)/i,
        found = str.match(re);

    console.dir(found);
}

match1();

运转效果:

 * Array[3]
 * 0: "Chapter 3.4.5.1"
 * 1: "Chapter 3.4.5.1"
 * 2: ".1"
 * index: 26
 * input: "For more information, see Chapter 3.4.5.1"
 * length: 3
 *
 * [input]:
 *         which contains the original string that was parsed
 * [0]:
 *     the first match
 * [1]:
 *     the first value remembered from (Chapter \d+(\.\d)*).
 * [2]:
 *     ".1" is the last value remembered from (\.\d).
 * ---------------------------------------------------
 */
function match2() {
    var str = "For more information, see Chapter 3.4.5.1",
        re = /(chapter \d+(\.\d)*)/gi,
        found = str.match(re);

    console.dir(found);
}

match2();

运转效果:

 * Array[1]
 * 0: "Chapter 3.4.5.1"
 * length: 1
 *
 * 假如使用了全局婚配g, 则只显示婚配到的悉数字符串。
 * 也没有input属性
function match3() {
    var str = "qbylucky@gmail.com",
        re = /(.*)@(.*)\.(.*)/,
        found = str.match(re);

    console.dir(found);
}

match3();

运转效果:

 * Array[4]
 * 0: "qbylucky@gmail.com"
 * 1: "qbylucky"
 * 2: "gmail"
 * 3: "com"
 * index: 0
 * input: "qbylucky@gmail.com"
 * length: 4
function match4() {
    var str = "qbylucky@gmail.com",
        re = /(.*)@(.*)\.(.*)/g,
        found = str.match(re);

    console.dir(found);
}

match4();

运转效果:

 * Array[1]
 * 0: "qbylucky@gmail.com"
 * length: 1
    原文作者:QETHAN
    原文地址: https://segmentfault.com/a/1190000000500300
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞