The second parameter of the string replace method in JavaScript explore _javascript tips

Source: Internet
Author: User
Tags anonymous

Objective

Replace the first argument generally places a regular expression that matches the text you want to replace, and the second argument, in general, we put in a string that replaces the text that is being matched.

In fact, the replace is far more powerful than the above, and its interior has been packaged very well, far from what we think is so simple, below we'll talk about the second parameter of Replace.

Example Introduction

Now to implement a function, convert the character entities in HTML into their corresponding characters, such as: "<" converted to the corresponding character: "<".

Now let's look at the code implementation:

string.prototype.deentityfy= (function () {
  var entity = {
  lt: ' < ',
  GT: ' > '
  };
  return function () {return
  this.replace/& ([^&;] +);/g,
   function (a,b,c,d) {
   console.log (' A: ' +a+ '---B: ' +b+ '---C: ' +c+ '---d: ' +d);
   var r = entity[b];
   return typeof r = = ' String '? R:a
   }}}
 ())
 document.write ('  
 

Here we have to talk about character entities to avoid confusion below.

Character entities are not parsed as HTML statements, such as the above '

  

Let's take a step-by-step look at how the above code is implemented.

First, String a method is mounted on the object's prototype, which deentityfy is a self executing function, using the form of closures, in which all content within the function is not visible externally.

function, we define an entity object literal, which has two attributes, corresponding to the left and right angle brackets of the label.

returnand then an anonymous function that writes what we're going to do in this function.

We call replace the method, and the first argument places the regular expression:

/& ([^&;] +);/g

The matching rule for this regular expression is: Start with "&", followed by one or more than "&" and ";" character, to ";" End. The following G represents a global match. (For more on regular expressions, click here)

It is emphasized here that the concept of "()" in regular "()" and "()" is different in JavaScript, where the parentheses term is called: capturing parentheses. Simply put, the characters in parentheses are stored for a short period of time and can be taken out of the replacement link.

Next, we'll talk about the second parameter of replace, which is the focus of this article.

We usually put a string in the second argument, it is used directly to replace the matched character; there are more advanced techniques, using the $ character to get the content in the capture bracket, and then do the related operation.

There is also a third way to pass in an anonymous function to the second parameter, and the return value of the function is used as the replacement character.

The anonymous function has 4 parameters that can be passed in, but none of these parameters must be passed.

First parameter: The character to which the regular match;

Second parameter: captures the characters captured by parentheses;

Third parameter: The index of the first character of each character that is matched to;

Fourth parameter: The string body used for the match;

I'm going to print out the results of the above code and we should be clear:

  

Summarize

Well, the above is the entire content of this article, contact is also nearly three years time, has not known that there is such a powerful function, of course, may be I am ignorant. I hope everyone will keep a learning heart and progress together. At the same time also hope that the content of this article for everyone's study or work can bring certain help, if there is doubt you can message exchange.

Related Article

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.