Javascript 正則使用第一篇

QETHAN發表於2014-05-10

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+)(+)/, was given, p1 is the match for a+, and p2 for +.

  • @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

相關文章