Replace () method in jQuery _ jquery

Source: Internet
Author: User
This article mainly introduces the replace () method usage in jQuery. It analyzes the functions, definitions, and matching elements of the replace () method to replace specified content, when reading jquery source code, you may find that the second parameter of replece () is a function that you have never noticed before, previously, I only knew that the second parameter of replace () can be a function, but I don't know how to operate it. Today, when the source code uses the function as the second parameter of replace, I felt it was hard to read the function, so I was prepared to sort out this function...

Syntax

StringObject. replace (regexp/substr, replacement)

Return Value

Returns a new string, which is obtained after the first match or token match of regexp is replaced by replacement.

When the replacement parameter of the replace () method is a function, in this case, each match calls this function, and the string returned by the function is used as the replacement text. The first parameter of this function is a matching string. The following parameter is a string that matches the subexpression in the pattern. There can be 0 or more such parameters. The following parameter is an integer that declares the position where the matching occurs in the stringObject. The last parameter is stringObject itself. This speech was copied to w3cschool. For now, I am not very clear about the above words, nor can I simply describe them with my own words, so we can only use instances to describe all this.

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 (second non-capturing Group)
Console. log (str4); // 5 (matching position in string)
Console. log (str5); // abc123-ii (string itself)
Return "I ";
})

Now I am reading the jquery source 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, I don't know when I used to use repleace (). It was a strange symbol for me at that time, like "$1, $2 "and so on. Now I have come to answer this question at night.

$1, $2, $3... indicates capturing 1, 2, 3 ....

The Code is as follows:


Var string = "abc123-ii ";
Console. log (string. replace (/(\ d)-([\ da-z])/g, "$1"); // use capture group 1 (3) replace/(\ d)-([\ da-z])/g

 

$ & Indicates the substring that matches regexp

The Code is as follows:


Var string = "abc123-ii ";
Console. log (string. replace (/(\ d)-([\ da-z])/g, "$ &"); // use a string that matches regexp (3-i) replace/(\ d)-([\ da-z])/g

$ 'Indicates the text on the left of the matched substring.

The Code is as follows:


Var string = "abc123-ii ";
Console. log (string. replace (/(\ d)-([\ da-z])/g, "$ '"); // match the text on the left of the string (abc12) replace/(\ d)-([\ da-z])/g

$ 'Indicates the text on the right of the matched substring.

The Code is as follows:


Var string = "abc123-ii ";
Console. log (string. replace (/(\ d)-([\ da-z])/g, "$ '")); // replace/(\ d)-([\ da-z])/g with the text on the right of the matching string

  

$ Directly as $ symbol

The Code is as follows:


Var string = "abc123-ii ";
Console. log (string. replace (/(\ d)-([\ da-z])/g, "$"); // replace/(\ d) with the $ symbol) -([\ da-z])/g

The above is my own use of the replace () method is not clear, I am a little white front-end, if there is something wrong with writing, or you may want to share some examples of better usage of this method...

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.