Home > Web Front-end > JS Tutorial > A brief discussion on the replace() method in jQuery_jquery

A brief discussion on the replace() method in jQuery_jquery

WBOY
Release: 2016-05-16 15:59:19
Original
2094 people have browsed it

When I was reading the jquery source code today, I found a problem that I had never noticed before, which is the problem when the second parameter of replace() is a function. I used to know that the second parameter of replace() can be a function, but I don’t know how to operate it. When I saw a function used as the second parameter of replace() in the source code today, I felt that it was difficult to read, so I planned to sort out this function...

Grammar

stringObject.replace(regexp/substr, replacement)

Return value

Returns a new string obtained by replacing the first match or any desired match of regexp with replacement

When the parameter replacement of the replace() method is a function, in this case, the function is called for each match, and the string returned by the function is used as the replacement text. The first parameter of this function is a string matching the pattern. The next parameter is a string that matches the subexpression in the pattern. There can be 0 or more such parameters. The next parameter is an integer that declares the position in stringObject where the match occurs. The last parameter is the stringObject itself. This sentence was copied from w3cschool. For me now, I don’t quite understand the above paragraph, and I can’t simply describe it in my own words, so I can only use examples to illustrate it all

Copy code The code is as follows:

var string = "abc123-ii";
string.replace(/(d)-([da-z])/g,function( str1, str2, str3, str4, str5){
           console.log( str1 ); // 3-i
            console.log( str2 ); // 3 (first capture)
            console.log(str3);//i (the second non-capturing group)
console.log(str4);//5 (match the position that appears in string)
            console.log(str5);// abc123-ii (string itself)
          return "I";
})

The above is the jquery source code I was looking at today

Copy code The code is as follows:

camelCase: function( string ) {
           return string.replace( rmsPrefix, "ms-" ).replace( rdashAlpha, fcamelCase );
},
fcamelCase = function( all, letter ) {
         return letter.toUpperCase();
};

I feel like I understand this function now that I didn’t understand it at the time

Then now I remember that I don’t know when in the past, I encountered strange symbols when using replace(), which were strange symbols to me at that time, such as "$1, $2" and so on. Now Ye Lai has an answer to this question

$1, $2, $3.... means capturing 1, 2, 3....

Copy code The code is as follows:

var string = "abc123-ii";
console.log(string.replace(/(d)-([da-z])/g, "$1")); // Use capture group 1(3) to replace /(d)-([da-z ])/g

$& represents a substring matching regexp

Copy code The code is as follows:

var string = "abc123-ii";
console.log(string.replace(/(d)-([da-z])/g, "$&")); // Replace /(d with the string (3-i) that matches regexp )-([da-z])/g

$` represents the text located to the left of the matching substring

Copy code The code is as follows:

var string = "abc123-ii";
console.log(string.replace(/(d)-([da-z])/g, "$`")); // Replace /(d)- with the text on the left side of the matching string (abc12) ([da-z])/g

$' represents the text located on the right side of the matched substring

Copy code The code is as follows:

var string = "abc123-ii";
console.log(string.replace(/(d)-([da-z])/g, "$'")); // Replace /(d)-([ with the text on the right side of the matched string da-z])/g

$$ is directly the $ symbol

Copy code The code is as follows:

var string = "abc123-ii";
console.log(string.replace(/(d)-([da-z])/g, "$$")); // Use $ symbol to replace /(d)-([da-z])/ g

The above is where I am not clear about the use of the replace() method. I am a front-end novice. If there is something wrong in what I wrote, or if there are examples of better usage of this method, I hope you can share it...

The above is the entire content of this article, I hope you all like it.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template