Zccst
Regular expressions are often new knowledge points, a little accumulation of it, today focus on replace, the previous impression should be:
Str.replace (old,new);
The old is found from the original string and replaced with new.
Today it is found that the second parameter can also be a function, and the function has two parameters.
For example, the implementation details of $.camelcase in jquery.
var ret = $.camelcase (str);
jquery Source code:
// Convert dashed to CamelCase; used by the CSS and data modules // Microsoft forgot to hump their vendor prefix (#9572) function (String) { return string.replace (Rmsprefix, "ms-" ). Replace (Rdashalpha, fcamelcase);}
Parsing process:
// Matches dashed string for camelizing , Rdashalpha =/-([\da-z])/gi, // used by Jquery.camelcase as callback to replace () fcamelcase = function return letter.touppercase (); },string = "Margin-top" var a = String.Replace (Rdashalpha, Fcamelcase); Console.log (a);
Note that fcaemlcase is a function, and there are two parameters, and only the second parameter is used in practice.
What do these two parameters mean?
by Rdashalpha =/-([\da-z])/gi you know, to look for-X from the original string and replace it with the Fcamelcase function.
So the two parameter resolution of the Fcamelcase () function is to trim the string and match the result.
Print arguments:
["-T", "T", 6, "Margin-top"]
Resources:
2: The second argument is a function:
In ECMASCRIPT3 it is recommended to use a function method, implemented in JavaScript1.2. When the Replace method executes, the function is called every time, and the value is returned as the new value to replace.
The function parameters are specified:
The first parameter is a full-text ($&) of each match.
The intermediate argument is a subexpression that matches the string, with an unlimited number. ($i (i:1-99))
The second-to-last argument matches the position of the matching text string.
The last parameter represents the string itself.
This is what this article wants to say replace powerful place, the theory of things are dry, we need examples to solve all the empty problems:
Example 1: Capitalize the first letter of the string:
function () { returnthisfunction(M,P1,P2) { console.log (arguments); return p1+p2.touppercase (); } ;
Print arguments:
["I", " " ", " i ", 0, " I am a boy! ")
["A", "" ", " A ", 1, " I am a boy! ")
["A", "" ", " A ", 4, " I am a boy! ")
["B", "", "B", 6, "I am a boy!")
Output: I Am A boy!
Example 2: On the string "Zhang 356 points, Li 474 points, Wang 592 points, Zhao 684 points" score extraction summary, calculate the average score and output the average difference of each person.
vars = "Zhang 356 points, Li 474 points, Wang 592 points, Zhao 684 points";varA = S.match (/\d+/g);varsum = 0; for(vari = 0; i < a.length; i++) {sum+=parsefloat (A[i]);}varAVG = SUM/a.length;functionf () {varn = parsefloat (arguments[1]); returnn + "min" + "(" + (n > Avg)? ("Out-of-average" + (N-AVG)): ("Less than average" + (avg-n)) + "Min");}varresult = S.replace (/(\d+) Min/G, f); Console.log (result);
Output:
356 points (below the average of 20.5 points), Li 474 points (below the average of 2.5 points), Wang 592 points (exceeding the average score of 15.5 points), Zhao 684 points (exceeding the average of 7.5 points)
With JavaScript's replace function plus regular advanced applications, JavaScript's replace will send back more power, where it will no longer delve into regular advanced application assertions.