A special use of string.replace in JavaScript

Source: Internet
Author: User

1 <!DOCTYPE HTML>2 <HTMLxmlns= "http://www.w3.org/1999/xhtml">3 <Head>4 <Metahttp-equiv= "content-type"content= "text/html; charset=utf-8"/>5     <title></title>6     <Scripttype= "text/javascript">7         /*8 This code is to add a Deentityfy method to the JavaScript string object,9 To replace the HTML characters in the string (replace &quot; with ",&lt; Replace with <,&gt; Replace with >),Ten                            */ one  a         //to add a custom method to the prototype of a string object - String.prototype.LiminReplace= function () { -             //to define a JSON object to replace the             varRpljson= { -                 //"first ": "quot", -                 //" Second": "lt", -                 //" third": "gt" +                 "quot": '"', -                 "LT": "<", +                 "GT": ">" a             }; at             return function () { -                 //The regular expression is detailed: -                 //[^&;] + occurrences of one or more characters except & and; -                 //([^&;] +) make a group of it -                 //& ([^&;]   +); To begin with &; -                 return  this. Replace (/& ([^&;] +);/g,function(a, b, c, d) { in                     //Let's print out the parameters of the method and see what the results are . -                      for (varI= 0; I<arguments.length; i++) { to Console.log (arguments[i]+ '<br/>'); +                     } - Console.log ('===========================<br/>'); the  *  $                     //detailed:Panax Notoginseng                     //the b parameter, which corresponds to the contents of a matched grouping in a regular expression, that is, the contents of the first parenthesis -                     //example: the                     //if the string passed in is &quot; The first packet that the regular match gets is the quot, which is exactly the key value of the Rpljson object +                     //if the string passed in is &lt; That regular match gets the first packet of the content is lt, which is exactly the key value of the Rpljson object a                     //if the string passed in is &gt; The first packet that the regular match gets is the gt, which is exactly the key value of the Rpljson object the                     varRplstr=rpljson[b]; +  -                     //detailed: $                     //typeof (rplstr) = = ' String ' to determine if the value type of RPLSTR is not a string $                     //Why do you have to judge that? -                     //var rplstr = rpljson[b]; Parameter B as key in Rpljson may not exist, does not exist, the value taken out is certainly not a string type -                     //returns the value corresponding to key in the Rpljson object if key exists, otherwise the original string is returned without substitution the                     return typeof(rplstr)== 'string' ?rplstr:a; -                 });Wuyi             } the         }(); //It's self-executing here, or It's a return Function. -  wu  - window.onload= function () {             about document.write ("My name is &quot; limin&quot;, Age &lt;20, My wages &gt;10000". Liminreplace ()); $             //results: -             //My name is "limin", age <20 -  -             //quot was executed two times replaced, printed two times, LT was executed a replacement, printed only once, the GT was executed once replaced, printed only once a             //function (a, b, c, d) parameter Description +             //the first argument is simple, it's a matching string the  -             //the second one is weird, but it's not hard to see it again. the second argument is the matching content within the parentheses of the regular expression (grouped Content) $  the             //the third argument and the easy-to-think, is that the match is the index in the string the  the             //The fourth argument is the original string the  -         } in     </Script> the </Head> the <Body> about  the </Body> the </HTML>

Printing results:


A special use of string.replace in JavaScript

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.