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 " with ",< Replace with <,> 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 " 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 < 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 > 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 " limin", Age <20, My wages >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