The Replace function method in JavaScript is to return a copy of a string that is replaced with text based on a regular expression. How to use: where stringobj is a required option. The string object or string literal to perform the substitution.
Recently browsing some of Ali's front-end interview questions, one of which involves the use of the Replace () method in JavaScript, the following is the original question:
"What is the role of the following function? What should I fill out in a blank area?"
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18 |
Define (function (window) {function fn (str) {this.str = str;} Fn.prototype.format = function () {var arg = ___; Return This.str.replace (_, function (A, b) {return arg[b] | | ''; }); } Window.fn = fn; }) (window); Use (function () {var t = new fn (' <p><a href= ' {0} ' >{1}</a><span>{2}</span></p> ') ; Console.log (T.format (' http://www.alibaba.com ', ' Alibaba ', ' Welcome ')); })(); |
The following is the analysis process (feel or get a number, personally feel more organized)
1, because the subject also involves to other knowledge points, such as anonymous functions, prototypes and other knowledge points, but not the focus of this discussion.
2, according to the problem, we know that the source of the problem is similar to writing a template engine. Replace placeholders such as ' {1} ' in the template with the parameters passed to it. So ARG is supposed to be arguments. But!!! Since ARG is not an array, but rather a class array object (U_u), we need to do some conversion,
Copy code code as follows:
var arg=array.prototype.slice.call (arguments,0);
3, the equal sign to the right is the first empty answer. Having said so much, then the second empty is the point we're going to discuss. ~~~~~~ we all know that the second space is to find the placeholder by the regular, and replace it with a string within the ARG array according to the number in the placeholder, to tell the truth. The second parameter of the Replace method is a function rarely encountered, Generally we encounter all of this, look at the following code:
Copy code code as follows:
var pattern=/8 (. *) 8/;
var str= ' This is a 8baidu8 ';
document.write (str.replace (Pattern, ' $ '));
4. Since replace the second parameter is a function of less, then we will focus on the second parameter is a function of the case.
First, this is the syntax for the Replace function: Stringobject.replace (regexp/substr,replacement)
which regexp/substr required. The RegExp object that prescribes the string or pattern to be replaced. (Note that if the value is a string, it is used as the direct text pattern to retrieve instead of being converted to the RegExp object first.) ) Replacement required. A string value. Provides a function that replaces text or generates alternate text. Finally, a new string is returned, which is obtained after the first match of RegExp or all matches are replaced with replacement.
5. ECMAScript stipulates that the parameter replacement of the replace () method can be a function rather than a string. In this case, each match calls the function, and the string it returns replaces the text used. The first parameter represents the character that is matched to, the second parameter represents the minimum index position (regexp.index) of the character that is matched, and the third parameter represents the matched string (regexp.input).
6, so the second space can write this:/{(d+)}/g, placed in the statement is complete:
Copy code code as follows:
Return This.str.replace (/{(d+)}/g, function (A, b) {
return Arg[b] | | '';
});
{0} is substituted for arg[0 when performing the first match]
{1} is substituted for arg[1 when performing the first match]
{2} is substituted for arg[2 when performing the first match]
7, the above is the JS string method replace () the second parameter for the function of the explanation (there is not perfect place, the supplementary), of course, this test also solves the problem of ~~~~~
The above mentioned is the entire content of this article, I hope you can enjoy.