JavaScript function Replace deep understanding of _javascript techniques

Source: Internet
Author: User

The Replace function accepts two arguments, the first argument is a string or regular expression, and the first argument can also accept a string, or possibly a function.

First, we no longer need to say "I am a Boy" for the first parameter as a string. Replace ("Boy", "Girl"), output: "I am a Girl". What I want to say here is the case where the first argument is regular. For regular expressions, the substitution behavior is first determined by the global (global//g), and if all substitutions are replaced, the Non-global only replaces the first matching string. For example:

Copy Code code as follows:

"Ha ha". replace (/\b\w+\b/g, "he")//he

"Ha ha". replace (/\b\w+\b/, "he")//he ha

1: The second parameter is a string:

For regular replace, a special tag is specified for $:

1. $i (i:1-99): Represents the text that matches a left-to-right subexpression.
2.$&: Represents full text that matches a regular expression.
3.$ ' (': Toggle Skill Key): Indicates the left text of the matching string.
4.$ ' (': single quote): The right text that represents the matching string.
5.$$: Represents the $ transfer.
Here are a few demo:

Copy Code code as follows:

"Boy & Girl". Replace (/(\w+) \s*&\s* (\w+)/g, "$ & $")//girl & Boy

"Boy". Replace (/\w+/g, "$&-$&")//Boy-boy

"JavaScript". Replace (/script/, "$&!= $")//javascript!= Java

"JavaScript". Replace (/java/, "$&$")//JavaScript is script

2: The second parameter is a function:

The ECMASCRIPT3 is recommended for use in the JavaScript1.2. The function is called every time the Replace method executes, returning the value as the new value for substitution.

Parameters of the function:

1. The first parameter is a full-text copy of each match ($&).
2. The intermediate parameter is the subexpression match string, the number is not limited. ($i (i:1-99))
3. The penultimate argument is the matching subscript position for the matching text string.
4. The last parameter represents the string itself.
This is what this article says about replace power, the theory is dry goods, we need examples to solve all the empty problems:

1: Capitalize the first letter of the string:

Copy Code code as follows:

String.prototype.capitalize = function () {

Return This.replace (/(^|\s) ([A-z])/g, function (M,P1,P2) {return p1+p2.touppercase ();

} );

};
Console.log ("I am a boy!"). Capitalize ())

Output: I Am A Boy!

2: The string "Zhang 356 points, Lee 474 points, Wang 592 points, Zhao 684 points" of the score extraction summary, calculate the average points and output everyone's average score gap.

Copy Code code as follows:

var s = "Zhang 356 points, Lee 474 points, Wang 592 points, Zhao 684 points";

var a = S.match (/\d+/g);

var sum = 0;

for (var i = 0; i < a.length; i++) {

Sum + + parsefloat (A[i]);

}



var avg = sum/a.length;



function f () {

var n = parsefloat (arguments[1]);

return n + "minute" + "(+ (n > Avg)?" ("Beyond average" + (N-AVG)):

("Below average score" + (AVG-N)) + "cent)";

}



var result = S.replace (/(\d+) minute/g, F);

Console.log (result);

Output:

Zhang 356 points (below the average of 20.5 points), Lee 474 points (below the average of 2.5 points), Wang 592 points (exceeding the average of 15.5 points), Zhao 684 points (exceeding the average of 7.5 points)

The Replace function of JavaScript plus the advanced application of the regular, JavaScript replace will send back a greater power, where it will no longer delve into the regular advanced application assertions.

Source: http://www.cnblogs.com/whitewolf/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.