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.
Copy codeThe Code is as follows: document. write ('20140901'. replace (1, 'x'); we can get the result: X234, which is normal,
Copy codeThe 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 ExpressionCopy codeThe Code is as follows: document. write ('123'. replace (/1/g, 'x'); at this time, we can get the expected result: X2X4
Let's take a look at function writing.Copy codeThe 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.
Copy codeThe 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 "with", <with <,> 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 above, 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
Copy codeThe 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] + '<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.Copy codeThe 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.
Copy codeThe Code is as follows: var r = '1a21b21c2 '. replace (/1 \ w2/g, function (){
For (var I = 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.
Copy codeThe 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.
Copy codeThe Code is as follows: var r = '1a21b21c2 '. replace (/1 (\ w2)/g, function (){
For (var I = 0; I <arguments. length; I ++ ){
Document. write (arguments [I] + '<br/> ');
}
Document. write ('=========================< br/> ')
Return 'X ';
});
Document. write (r );
View results
Copy codeThe 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?
Copy codeThe Code is as follows: // there are a lot of methods. This is just to verify that our theory was deliberately written as 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 );
Copy codeThe Code is as follows: // This write 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 );