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 fully understand it. I understood the usage of replace, the original replace () method is not as simple as imagined. It is used to replace 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.
The Code is as follows:
Document. write ('20140901'. replace (1, 'x'); we can get the result: X234, which is normal,
The Code is as follows:
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, then replace the first match to stop searching.
Let's Replace the Regular Expression
The Code is as follows:
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.
The Code is as follows:
Var r = '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.
The Code is as follows:
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 ('<">'. deentityfy ());
This Code adds a deentityfy Method to the String object of JavaScript to replace the HTML characters in the String (replace "", <替换为<,> 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
The Code is as follows:
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] +'
');
}
Document. write ('==============================
');
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.
The Code is as follows:
A2
A
A21b21c2
======================================
B2
A21b21c2
======================================
C2
C
A21b21c2
======================================
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.
The Code is as follows:
Var r = '1a21b21c2 '. replace (/1 \ w2/g, function (){
For (var I = 0; I <arguments. length; I ++ ){
Document. write (arguments [I] +'
');
}
Document. write ('==============================
')
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.
The Code is as follows:
A2
A21b21c2
======================================
B2
A21b21c2
======================================
C2
A21b21c2
======================================
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.
The Code is as follows:
Var r = '1a21b21c2 '. replace (/1 (\ w2)/g, function (){
For (var I = 0; I <arguments. length; I ++ ){
Document. write (arguments [I] +'
');
}
Document. write ('==============================
')
Return 'X ';
});
Document. write (r );
View results
The Code is as follows:
A2
A2
A21b21c2
======================================
B2
B2
A21b21c2
======================================
C2
C2
A21b21c2
======================================
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?
The Code is as follows:
// There are many methods. This is just to verify that our theory was deliberately written into this troublesome practice.
Var sentence = 'I love you ';
Var upper = sentence. replace (/(\ w) \ w * \ B/g, function (a, B ){
Return B. toUpperCase () + a. substring (1 );
});
Document. write (upper );
The Code is as follows:
// This writing is actually competent
Var sentence = 'I love you ';
Var upper = sentence. replace (/\ w + \ B/g, function (){
Return a. substr (0, 1). toUpperCase () + a. substring (1 );
});
Document. write (upper );