When using JavaScript to process strings, we often use the replace method. This is a very simple method, which has never been ignored before, today, when I read an interesting small example of the Javascript language, I didn't quite understand it. I understood how to use replace. It turns out that replace is not as simple as I thought.
The Replace () method is used to replace other characters with some characters in a string, or replace a substring that matches a regular expression.
Syntax: String. Replace (substr/Reg, replacestr/function)
The first parameter can be a substring of a string or a regular expression. The second parameter can be a string or a processing method. Let's take a look at it separately.
Document. Write ('20140901'. Replace (1, 'x '));
We can get the result: x234, which is normal,
Document. Write ('20140901'. Replace (1, 'x '));
The expected result is: x2x4, but the result is: x214. That is to say, if the first parameter is a substring, replace will stop searching only when the first match is replaced.
Let's Replace the Regular Expression
Document. Write ('20140901'. Replace (/1/g, 'x '));
At this time, we can get the expected result: x2x4
Let's take a look at function writing.
VaRR = 'abcd'. Replace (/\ W/g,Function(){Return'X';}); Document. Write (R );
Now we can see the expected result: xxxx, all characters are replaced with X, which is my previous understanding of replace, but I am confused when I see such an example in the essence of JavaScript language.
VaR T = Document. getelementbyid ('T' ); String. Prototype. deentityfy = Function (){ VaR Entity = {Quot: '"' , LT: '<', GT: '>' }; Return Function (){ Return This . Replace (/& ([^ &;] +);/g, Function (A, B ){ VaR R = Entity [B]; Return Typeof R = 'string '?R: ;}); // End of replace };} (); Document. Write ( '& Lt; & quot; & gt;'. deentityfy ());
This sectionCodeIs to add a deentityfy Method to the string object of JavaScript to replace the HTML characters in the string (replace & quot; with ", & lt; with <, & gt; replace with>). First, we ignore the language skills used by the author to see how his replace is used. The first parameter is a regular expression that matches the three strings mentioned earlier, the function of the second parameter actually has two parameters. What are these two parameters? Why is the method expected? Let's analyze it briefly.
Entity [B] is the usage of JavaScript-related arrays. The value is obtained based on the name of the array data. To facilitate understanding, we may wish to modify this method to make it easier, so that we can see more clearly what the function parameter is, and to eliminate the browser transcoding problem, let's modify and replace the string
String. Prototype. deentityfy = function (){
VaR entity = {
A: 'A ',
B: 'B ',
C: 'C'
};
Return function (){
Return this. Replace (/1 ([^ 12]) 2/g, function (a, B ){
For (VAR I = 0; I <arguments. length; I ++ ){
Document. Write (arguments [I] + '<br/> ');
}
Document. Write ('=========================< br/> ');
VaR r = entity [B];
Return typeof R === 'string '? R:;
}); // End of replace
};
}();
Document. Write ('1a21b21c2 '. deentityfy ());
In this way, we print all the parameters of the method to see what the result is.
1A2
A
0
1a21b21c2
======================================
1B2
B
3
1a21b21c2
======================================
1c2
C
6
1a21b21c2
======================================
ABC
It's strange, right. The last <"> is the result of the method, which is correct and gets the expected result. Let's look at the function parameter section,
The function is called three times, which is exactly the number of matching times. Each time it replaces the matching string.
Each call has four parameters.
The first parameter is a string matching.
The second one is very strange, but it is not difficult to get it from every reading. The second parameter is the Matching content in the regular expression brackets.
The third parameter and easy to think of is the index of the matching item in the string.
The fourth parameter is the original string.
It's amazing, right? But that's not it. Let's try again.
VaRR = '1a21b21c2 '. Replace (/1 \ W2/g,Function(){For(VaRI = 0; I <arguments. length; I ++) {Document. Write (arguments [I]+ '<Br/>');} Document. Write ('===========================< Br/>')Return'X';}); Document. Write (R );
It is similar to the previous example, but simply replaces all the matching items with X to see the result.
1a201a21b21c2 ====================== 1b231a21b21c2 ========== ================== 1c261a21b21c2 =========================== ==== xxx
Unexpectedly, the result is expected, but the parameter is missing, and the second parameter is missing. Let's see what's different. The seemingly redundant parentheses in the regular expression are gone, in the previous example, the second parameter is exactly the matching item in the brackets. Is the second parameter a matching item in the brackets of the regular expression? We will add the brackets to verify it.
VaRR = '1a21b21c2 '. Replace (/1 (\ W2)/g,Function(){For(VaRI = 0; I <arguments. length; I ++) {Document. Write (arguments [I]+ '<Br/>');} Document. Write ('===========================< Br/>')Return'X';}); Document. Write (R );
View results
1a2a201a21b21c2 ======================== 1b2b231a21b21c2 ============== =====================1c2c261a21b21c2 ============================ ==== xxx
Sure enough, so we can understand the parameters in the function. Now we can see the example of the unique JavaScript language. Of course, we need to know the joined array and execute the function immediately, closure and arguments object. If we want to upper the first letter of all words in a sentence, will it?
//There are many methods. This is just to verify that our theory was deliberately written into this troublesome practice. VaRSentence = 'I love you';VaRUpper = sentence. Replace (/(\ W) \ W * \ B/g,Function(A, B ){ReturnB. touppercase () + A. substring (1) ;}); Document. Write (upper );
// in this way, writing is actually competent var sentence = 'I love you' ; var upper = sentence. replace (/\ W + \ B/g, function (a) { return . substr (0, 1 ). touppercase () +. substring (1 ) ;}); document. write (upper);