Regular use of JavaScript

Time:2021-3-27

replace()

Parameter description

  • @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);   
    //Assign value here, in order to better observe the value and position of P1 and P3
    p1 = 1;
    p3 = 3;    
   return [p1, p2, p3].join(' - ');
}
newString = "111abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
console.log(newString);

Results of operation:

 * 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);

Results of operation:

 * 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);

Results of operation:

*First match

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

 *Second match

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


 *Third match

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


 *The fourth match

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

 *The fifth match

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

 *The sixth match

 * 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();

Results of operation:

 * 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();

Results of operation:

* Array[1]
 * 0: "Chapter 3.4.5.1"
 * length: 1
 *
 *If global matching G is used, only all the matched strings are displayed.
 *There is also no input attribute
function match3() {
    var str = "[email protected]",
        re = /(.*)@(.*)\.(.*)/,
        found = str.match(re);

    console.dir(found);
}

match3();

Results of operation:

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

    console.dir(found);
}

match4();

Results of operation:

 * Array[1]
 * 0: "[email protected]"
 * length: 1

Recommended Today

. net Maui preview 5 function Preview

Although Microsoft build has just passed, we will continue to share our continuous progress in. Net multi platform application UI (. Net Maui). In this release, we have enabled animation and view transformation, completed the migration of multiple UI components, and improved a single project template.We also released the first batch of preview documents covering […]